﻿using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using LSQL;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Spreadsheet;
using DevExpress.Xpf.Spreadsheet;
using DevExpress.Utils.Svg;
using System.Reflection;
using DevExpress.Drawing;
//using LPA.Properties;

namespace Shopfloormanagement
{
    public partial class Form_LPA : DevExpress.XtraEditors.XtraForm
    {
        public static Form_LPA frm = new Form_LPA();
        public int Lv_tag = 1;
        public Form_LPA()
        {
            InitializeComponent();
            frm = this;
            //SystemInfo.readFactory = 7;
            SystemInfo.readDepartment = 48;
            //SystemInfo.dbName = "SFM_MECYPiston";
        }
        private void Form_LPA_Load(object sender, EventArgs e)
        {
            LoadBasicInfo6();
            LoadGridview_List();
            LoadGridView_Analysis();
            LoadGridView_Analysis_Table();
            LoadItem();//加载Tcare的Item

            dtp1.DateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/01 00:00:00"));
            dtp4.DateTime = DateTime.Now;
            // 禁用筛选
            bandedGridView_MakePlan.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            bandedGridView_MakePlan.OptionsCustomization.AllowFilter = false;
            bandedGridView_MakePlan.OptionsCustomization.AllowSort = false;
            bandedGridView_Plan.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            bandedGridView_Plan.OptionsCustomization.AllowFilter = false;
            bandedGridView_Plan.OptionsCustomization.AllowSort = false;
            gridView_Analysis.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            gridView_Analysis.OptionsCustomization.AllowFilter = false;
            gridView_Analysis.OptionsCustomization.AllowSort = false;

            // 启用行标题自动高度调整
            bandedGridView_MakePlan.OptionsView.ColumnHeaderAutoHeight = DevExpress.Utils.DefaultBoolean.True;
            bandedGridView_Plan.OptionsView.ColumnHeaderAutoHeight = DevExpress.Utils.DefaultBoolean.True;
            // 启用行高自动调整
            bandedGridView_MakePlan.OptionsView.RowAutoHeight = true;
            bandedGridView_Plan.OptionsView.RowAutoHeight = true;

            tab_LPA.ShowTabHeader = DevExpress.Utils.DefaultBoolean.False;
            tab_LPA2.ShowTabHeader = DevExpress.Utils.DefaultBoolean.False;
            tab_Tcard.ShowTabHeader = DevExpress.Utils.DefaultBoolean.False;
            gridView_Member.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;//绘制列标题网格线颜色
            gridView_Audit.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;//绘制列标题网格线颜色
            gridView_Tcard.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;//绘制列标题网格线颜色
            gridView_Analysis.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;//绘制列标题网格线颜色
            gridView_List.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;//绘制列标题网格线颜色

            bandedGridView_Plan.CustomDrawBandHeader += bandedGridView_Plan_CustomDrawBandHeader;//绘制列标题网格线颜色
            bandedGridView_MakePlan.CustomDrawBandHeader += bandedGridView_Plan_CustomDrawBandHeader;//绘制列标题网格线颜色

            lb_Active2.Click += lb_Active1_Click;
            lb_Overdue2.Click += lb_Overdue1_Click;
            lb_Total2.Click += lb_Total1_Click;
            lb_PlantTotal2.Click += lb_PlantTotal1_Click;
            btn_Plan_Click(sender, e);

            gridView_Analysis.OptionsView.ShowIndicator = false;

            bandedGridView_Plan.BandPanelRowHeight = 40;
            bandedGridView_MakePlan.BandPanelRowHeight = 40;

            bandedGridView_Plan.OptionsSelection.EnableAppearanceFocusedRow = false; // 禁用焦点行高亮
            bandedGridView_MakePlan.OptionsSelection.EnableAppearanceFocusedRow = false; // 禁用焦点行高亮
            bandedGridView_Plan.OptionsSelection.EnableAppearanceHideSelection = false; // 禁用失去焦点后的高亮
            bandedGridView_MakePlan.OptionsSelection.EnableAppearanceHideSelection = false; // 禁用失去焦点后的高亮
            if (Lv_tag == 1)
            {
                panelControl10.Visible = true;
            }
            else
            {
                panelControl10.Visible = false;
            }
        }
        private void gridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
        {
            // 先让默认绘制生效
            e.DefaultDraw();

            // 获取列信息
            var column = e.Column;
            if (column == null) return;

            // 计算右侧分割线位置
            int x = e.Bounds.Right - 1;

            // 绘制垂直线（颜色、位置、粗细）
            using (var pen = new Pen(Color.White, 2))
            {
                e.Graphics.DrawLine(pen, x, e.Bounds.Top, x, e.Bounds.Bottom);
            }
        }
        private void bandedGridView_Plan_CustomDrawBandHeader(object sender, BandHeaderCustomDrawEventArgs e)
        {
            var view = sender as BandedGridView;
            if (view == null) return;

            // 清空默认绘制
            e.DefaultDraw();

            // 手动绘制红色网格线
            using (Pen pen = new Pen(Color.White, 1))
            {
                Rectangle bounds = e.Bounds;

                // 绘制右侧垂直线
                e.Graphics.DrawLine(pen, bounds.Right - 1, bounds.Top, bounds.Right - 1, bounds.Bottom);
                // 绘制底部水平线
                e.Graphics.DrawLine(pen, bounds.Left, bounds.Bottom - 1, bounds.Right, bounds.Bottom - 1);
            }

            e.Handled = true; // 阻止后续绘制
        }
        public class ComboBoxItem
        {
            public string DisplayName { get; set; }
            public object Tag { get; set; }

            // 构造函数，接受两个参数
            public ComboBoxItem(string displayName, object tag)
            {
                DisplayName = displayName;
                Tag = tag;
            }

            public override string ToString()
            {
                return DisplayName; // 这将用于显示在ComboBox中
            }
        }
        //通过DisplayName获取所在combobox内的索引
        public int FindComboBoxItemIndex_DisplayName(ComboBoxEdit comboBox, string targetDisplayName)
        {
            for (int i = 0; i < comboBox.Properties.Items.Count; i++)
            {
                if (comboBox.Properties.Items[i] is ComboBoxItem item && item.DisplayName == targetDisplayName)
                {
                    return i;
                }
            }
            return -1;
        }
        //通过Tag获取所在combobox内的索引
        public int FindComboBoxItemIndex_Tag(ComboBoxEdit comboBox, string targetTag)
        {
            for (int i = 0; i < comboBox.Properties.Items.Count; i++)
            {
                if (comboBox.Properties.Items[i] is ComboBoxItem item && item.Tag.ToString() == targetTag)
                {
                    return i;
                }
            }
            return -1;
        }

        #region 主页    
        int Mark = 0;
        private void lb_Active1_Click(object sender, EventArgs e)
        {
            tab_LPA2.SelectedTabPageIndex = 0;
            palActive.BackColor = Color.FromArgb(146, 208, 80);
            palOverdue.BackColor = Color.Transparent;
            palTotal.BackColor = Color.Transparent;
            palPlantTotal.BackColor = Color.Transparent;
            Mark = 1;
            LoadResultList();
        }
        private void lb_Overdue1_Click(object sender, EventArgs e)
        {
            tab_LPA2.SelectedTabPageIndex = 0;
            palActive.BackColor = Color.Transparent;
            palOverdue.BackColor = Color.FromArgb(146, 208, 80);
            palTotal.BackColor = Color.Transparent;
            palPlantTotal.BackColor = Color.Transparent;
            Mark = 2;
            LoadResultList();
        }
        private void lb_Total1_Click(object sender, EventArgs e)
        {
            tab_LPA2.SelectedTabPageIndex = 0;
            palActive.BackColor = Color.Transparent;
            palOverdue.BackColor = Color.Transparent;
            palTotal.BackColor = Color.FromArgb(146, 208, 80);
            palPlantTotal.BackColor = Color.Transparent;
            Mark = 3;
            LoadResultList();
        }
        private void lb_PlantTotal1_Click(object sender, EventArgs e)
        {
            tab_LPA2.SelectedTabPageIndex = 0;
            palActive.BackColor = Color.Transparent;
            palOverdue.BackColor = Color.Transparent;
            palTotal.BackColor = Color.Transparent;
            palPlantTotal.BackColor = Color.FromArgb(146, 208, 80);
            Mark = 4;
            LoadResultList();
        }
        private void btn_Member1_Click(object sender, EventArgs e)
        {
            LoadMember();
            tab_LPA.SelectedTabPageIndex = 1;
        }
        private void btn_Audit1_Click(object sender, EventArgs e)
        {
            LoadAuditScope();
            tab_LPA.SelectedTabPageIndex = 2;
        }
        private void btn_Edit_Click(object sender, EventArgs e)
        {
            Empty6();
            dtp61.ReadOnly = false;
            cmb_Auditor6.ReadOnly = false;
            labelControl19.Text = "Submit the results";
            tab_LPA.SelectedTabPageIndex = 5;
        }
        private void btn_MakePlan_Click(object sender, EventArgs e)
        {
            LoadBasicInfo();
            tab_LPA.SelectedTabPageIndex = 3;
            LoadPlan2();
        }
        private void btn_Plan_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
            tab_LPA2.SelectedTabPageIndex = 1;
            palActive.BackColor = Color.Transparent;
            palOverdue.BackColor = Color.Transparent;
            palTotal.BackColor = Color.Transparent;
            palPlantTotal.BackColor = Color.Transparent;
            LoadPlan1();

            DateTime dateTime1 = Convert.ToDateTime(dtp1.DateTime.ToString("yyyy/MM/01"));
            DateTime dateTime2 = Convert.ToDateTime(dtp1.DateTime.ToString("yyyy/MM/01")).AddMonths(1);
            dt_list = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_ResultList", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date>='" + dateTime1 + "' and Date<'" + dateTime2 + "' and Del_tag=0");
        }
        private void btn_Tcard_Click(object sender, EventArgs e)
        {
            LoadTcardType();
            tab_LPA.SelectedTabPageIndex = 4;
            labelControl18.Text = "";
            labelControl18.Tag = "";
            tab_Tcard.SelectedTabPageIndex = 0;
            tab_Tcard.Visible = false;
        }
        private void btn_Analysis_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 6;
            Analysis();
        }
        private void tab_LPA2_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (tab_LPA2.SelectedTabPageIndex == 0)
            {
                panelControl5.Visible = false;
            }
            else if (tab_LPA2.SelectedTabPageIndex == 1)
            {
                panelControl5.Visible = true;
            }
        }
        #endregion

        #region 主页-问题清单
        private void LoadGridview_List()
        {
            gridView_List.Columns.Clear();
            //创建选择图片格式
            RepositoryItemImageComboBox ImageComboBox = new RepositoryItemImageComboBox();
            ImageComboBox.SmallImages = imageList1;
            ImageComboBox.Items.Add(new ImageComboBoxItem("", 0, 0));
            ImageComboBox.Items.Add(new ImageComboBoxItem("", 1, 1));
            ImageComboBox.Items.Add(new ImageComboBoxItem("", 2, 2));
            ImageComboBox.Items.Add(new ImageComboBoxItem("", 3, 3));
            ImageComboBox.Items.Add(new ImageComboBoxItem("", 4, 4));

            RepositoryItemCheckEdit CheckEdit = new RepositoryItemCheckEdit
            {
            };

            GridColumn textColumn1 = new GridColumn();
            textColumn1.FieldName = "TextColumn1"; // 设置字段名
            textColumn1.Caption = "ID"; // 列标题
            textColumn1.Visible = true; // 显示列

            GridColumn textColumn2 = new GridColumn();
            textColumn2.FieldName = "TextColumn2"; // 设置字段名
            textColumn2.Caption = "Date"; // 列标题
            textColumn2.Visible = true; // 显示列

            GridColumn textColumn3 = new GridColumn();
            textColumn3.FieldName = "TextColumn3"; // 设置字段名
            textColumn3.Caption = "Auditor"; // 列标题
            textColumn3.Visible = true; // 显示列

            GridColumn textColumn4 = new GridColumn();
            textColumn4.FieldName = "TextColumn4"; // 设置字段名
            textColumn4.Caption = "Scope"; // 列标题
            textColumn4.Visible = true; // 显示列

            GridColumn textColumn5 = new GridColumn();
            textColumn5.FieldName = "TextColumn5"; // 设置字段名
            textColumn5.Caption = "Tcard"; // 列标题
            textColumn5.Visible = true; // 显示列

            GridColumn textColumn6 = new GridColumn();
            textColumn6.FieldName = "TextColumn6"; // 设置字段名
            textColumn6.Caption = "IfHaveProblem?"; // 列标题
            textColumn6.Visible = true; // 显示列
            //textColumn6.ColumnEdit = CheckEdit;

            GridColumn textColumn7 = new GridColumn();
            textColumn7.FieldName = "TextColumn7"; // 设置字段名
            textColumn7.Caption = "ProblemDescription"; // 列标题
            textColumn7.Visible = true; // 显示列

            GridColumn textColumn8 = new GridColumn();
            textColumn8.FieldName = "TextColumn8"; // 设置字段名
            textColumn8.Caption = "Responsible"; // 列标题
            textColumn8.Visible = true; // 显示列

            GridColumn textColumn9 = new GridColumn();
            textColumn9.FieldName = "TextColumn9"; // 设置字段名
            textColumn9.Caption = "ActionPlan"; // 列标题
            textColumn9.Visible = true; // 显示列

            GridColumn textColumn10 = new GridColumn();
            textColumn10.FieldName = "TextColumn10"; // 设置字段名
            textColumn10.Caption = "CompletionDate"; // 列标题
            textColumn10.Visible = true; // 显示列

            GridColumn textColumn11 = new GridColumn();
            textColumn11.FieldName = "TextColumn11"; // 设置字段名
            textColumn11.Caption = "Status"; // 列标题
            textColumn11.Visible = true; // 显示列
            textColumn11.ColumnEdit = ImageComboBox;

            GridColumn textColumn12 = new GridColumn();
            textColumn12.FieldName = "TextColumn12"; // 设置字段名
            textColumn12.Caption = "Comments"; // 列标题
            textColumn12.Visible = true; // 显示列

            //创建隐藏列用来存ID值
            GridColumn textColumn21 = new GridColumn();
            textColumn21.FieldName = "TextColumn21"; // 设置字段名
            textColumn21.Caption = "AuditorID"; // 列标题
            textColumn21.Visible = false; // 显示列
                                          //创建隐藏列用来存ID值
            GridColumn textColumn22 = new GridColumn();
            textColumn22.FieldName = "TextColumn22"; // 设置字段名
            textColumn22.Caption = "ResponsibleID"; // 列标题
            textColumn22.Visible = false; // 显示列

            textColumn1.Width = 30;
            textColumn2.Width = 50;
            textColumn6.Width = 30;
            textColumn10.Width = 50;
            textColumn11.Width = 30;

            gridView_List.Columns.Add(textColumn1);
            gridView_List.Columns.Add(textColumn2);
            gridView_List.Columns.Add(textColumn3);
            gridView_List.Columns.Add(textColumn4);
            gridView_List.Columns.Add(textColumn5);
            gridView_List.Columns.Add(textColumn6);
            gridView_List.Columns.Add(textColumn7);
            gridView_List.Columns.Add(textColumn8);
            gridView_List.Columns.Add(textColumn9);
            gridView_List.Columns.Add(textColumn10);
            gridView_List.Columns.Add(textColumn11);
            gridView_List.Columns.Add(textColumn12);
            gridView_List.Columns.Add(textColumn21);
            gridView_List.Columns.Add(textColumn22);

            // 创建数据源
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("TextColumn1", typeof(int));
            dataTable.Columns.Add("TextColumn2", typeof(DateTime));
            dataTable.Columns.Add("TextColumn3", typeof(string));
            dataTable.Columns.Add("TextColumn4", typeof(string));
            dataTable.Columns.Add("TextColumn5", typeof(string));
            dataTable.Columns.Add("TextColumn6", typeof(string));
            dataTable.Columns.Add("TextColumn7", typeof(string));
            dataTable.Columns.Add("TextColumn8", typeof(string));
            dataTable.Columns.Add("TextColumn9", typeof(string));
            dataTable.Columns.Add("TextColumn10", typeof(DateTime));
            dataTable.Columns.Add("TextColumn11", typeof(int));
            dataTable.Columns.Add("TextColumn12", typeof(string));
            dataTable.Columns.Add("TextColumn21", typeof(int));
            dataTable.Columns.Add("TextColumn22", typeof(int));

            gridControl_List.DataSource = dataTable;

            //gridView_List.Columns[0].OptionsColumn.AllowEdit = false;
            //gridView_List.Columns[1].OptionsColumn.AllowEdit = false;
            //gridView_List.Columns[2].OptionsColumn.AllowEdit = false;
            //gridView_List.Columns[3].OptionsColumn.AllowEdit = false;
            //gridView_List.Columns[4].OptionsColumn.AllowEdit = false;

            gridView_List.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            gridView_List.Columns[10].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            foreach (GridColumn gridColumn in gridView_List.Columns)
            {
                gridColumn.AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                gridColumn.AppearanceHeader.Font = new Font("Tahoma", 9, FontStyle.Bold);
            }
        }
        private void LoadResultList()
        {
            if (tab_LPA.SelectedTabPageIndex == 0 && tab_LPA2.SelectedTabPageIndex == 0)
            {
                //填充表格
                while (gridView_List.DataRowCount > 0) // 循环删除所有行
                {
                    gridView_List.DeleteRow(0);
                }

                DateTime dateTime1 = Convert.ToDateTime(dtp1.DateTime.ToString("yyyy/MM/01"));
                DateTime dateTime2 = Convert.ToDateTime(dtp1.DateTime.ToString("yyyy/MM/01")).AddMonths(1);

                //工厂级的数据表
                DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_ResultList", "AuditFactortyID=" + SystemInfo.readFactory + " and Date>='" + dateTime1 + "' and Date<'" + dateTime2 + "' and Del_tag=0");
                //工厂内某部门的表
                DataRow[] dr_Total = dt1.Select("AuditDepartmentID=" + SystemInfo.readDepartment);
                //工厂内某部门的  发现问题  未关闭  未超期
                DataRow[] dr_Active = dt1.Select("AuditDepartmentID=" + SystemInfo.readDepartment + " and Problem=1 and Status < 4 and CompletionDate>= '" + DateTime.Now.ToString("yyyy/MM/dd 00:00:00") + "'");
                //工厂内某部门的  发现问题  未关闭  已超期
                DataRow[] dr_Overdue = dt1.Select("AuditDepartmentID=" + SystemInfo.readDepartment + " and Problem=1 and Status < 4 and CompletionDate< '" + DateTime.Now.ToString("yyyy/MM/dd 00:00:00") + "'");

                lb_PlantTotal1.Text = dt1.Rows.Count.ToString();//工厂的total
                lb_Total1.Text = dr_Total.Length.ToString();//部门的total
                lb_Active1.Text = dr_Active.Length.ToString();//部门的  发现问题  未关闭  未超期
                lb_Overdue1.Text = dr_Overdue.Length.ToString();//部门的  发现问题  未关闭  已超期

                // 创建一个新的 DataTable，并克隆原始表的结构
                DataTable newTable = dt1.Clone();
                if (Mark == 1)
                {
                    foreach (DataRow row in dr_Active)
                    {
                        newTable.ImportRow(row);
                    }
                }
                else if (Mark == 2)
                {
                    foreach (DataRow row in dr_Overdue)
                    {
                        newTable.ImportRow(row);
                    }
                }
                else if (Mark == 3)
                {
                    foreach (DataRow row in dr_Total)
                    {
                        newTable.ImportRow(row);
                    }
                }
                else if (Mark == 4)
                {
                    newTable = dt1;
                }

                for (int i = 0; i < newTable.Rows.Count; i++)
                {
                    gridView_List.AddNewRow(); // 添加新行
                    int b = gridView_List.GetRowHandle(gridView_List.DataRowCount); // 获取新行的句柄
                    gridView_List.SetRowCellValue(b, "TextColumn1", newTable.Rows[i][0].ToString()); // 设置新行的列值
                    gridView_List.SetRowCellValue(b, "TextColumn2", newTable.Rows[i][1].ToString()); // 设置新行的列值
                    gridView_List.SetRowCellValue(b, "TextColumn3", newTable.Rows[i][2].ToString()); // 设置新行的列值    
                    gridView_List.SetRowCellValue(b, "TextColumn4", newTable.Rows[i][3].ToString()); // 设置新行的列值    
                    gridView_List.SetRowCellValue(b, "TextColumn5", newTable.Rows[i][4].ToString()); // 设置新行的列值    

                    if (newTable.Rows[i][5].ToString() == "0")
                    {
                        gridView_List.SetRowCellValue(b, "TextColumn6", "No"); // 设置新行的列值    
                    }
                    else if (newTable.Rows[i][5].ToString() == "1")
                    {
                        gridView_List.SetRowCellValue(b, "TextColumn6", "Yes"); // 设置新行的列值   
                    }
                    else
                    {
                        gridView_List.SetRowCellValue(b, "TextColumn6", "Be unable to audit"); // 设置新行的列值    
                    }
                    gridView_List.SetRowCellValue(b, "TextColumn7", newTable.Rows[i][6].ToString()); // 设置新行的列值    
                    gridView_List.SetRowCellValue(b, "TextColumn8", newTable.Rows[i][7].ToString()); // 设置新行的列值                 
                    gridView_List.SetRowCellValue(b, "TextColumn9", newTable.Rows[i][8].ToString()); // 设置新行的列值
                    if (newTable.Rows[i][9].ToString() != "")
                    {
                        gridView_List.SetRowCellValue(b, "TextColumn10", newTable.Rows[i][9].ToString()); // 设置新行的列值                
                    }
                    gridView_List.SetRowCellValue(b, "TextColumn11", newTable.Rows[i][10].ToString()); // 设置新行的列值                 
                    gridView_List.SetRowCellValue(b, "TextColumn12", newTable.Rows[i][11].ToString()); // 设置新行的列值    
                    gridView_List.SetRowCellValue(b, "TextColumn21", newTable.Rows[i][16].ToString()); // 设置新行的列值    
                    gridView_List.SetRowCellValue(b, "TextColumn22", newTable.Rows[i][17].ToString()); // 设置新行的列值    
                    gridView_List.UpdateCurrentRow(); // 更新当前行
                }
            }
        }
        private void gridView_List_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            if (e.HitInfo.Column != null)
            {
                if (e.HitInfo.InRow)
                {
                    // 显示PopupMenu并标记事件已处理
                    popupMenu2.ShowPopup(Control.MousePosition);
                }
            }
        }
        private void barButtonItem_Update_ItemClick(object sender, ItemClickEventArgs e)
        {
            int focusedRowHandle = gridView_List.FocusedRowHandle;
            string focusedColumnHandle = gridView_List.FocusedColumn.FieldName;
            string Auditor = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn3").ToString();
            string Responsible = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn8").ToString();
            string find = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn6").ToString();
            if (find == "No")
            {
                radioGroup1.SelectedIndex = 0;
            }
            else if (find == "Yes")
            {
                radioGroup1.SelectedIndex = 1;
            }
            else
            {
                radioGroup1.SelectedIndex = 2;
            }
            lb_ListID.Text = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn1").ToString();
            dtp61.DateTime = Convert.ToDateTime(gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn2").ToString());
            cmb_Auditor6.SelectedIndex = FindComboBoxItemIndex_DisplayName(cmb_Auditor6, Auditor);
            cmb_Responsible6.SelectedIndex = FindComboBoxItemIndex_DisplayName(cmb_Responsible6, Responsible);
            txt_Description6.Text = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn7").ToString();
            txt_ActionPlan6.Text = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn9").ToString();
            string datetime2 = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn10").ToString();
            if (datetime2 != "")
            {
                dtp62.DateTime = Convert.ToDateTime(datetime2);
            }
            imageComboBoxEdit1.SelectedIndex = Convert.ToInt32(gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn11").ToString());
            txt_Comments6.Text = gridView_List.GetRowCellValue(focusedRowHandle, "TextColumn12").ToString();

            dtp61.ReadOnly = true;
            cmb_Auditor6.ReadOnly = true;

            labelControl19.Text = "Update the results";
            tab_LPA.SelectedTabPageIndex = 5;
        }
        #endregion

        #region 主页-计划表
        DataTable dt_Plan;
        DateTime dateTime1;
        private void dtp1_EditValueChanged(object sender, EventArgs e)
        {
            if (dtp1.DateTime.ToString("yyyy/MM") != dateTime1.ToString("yyyy/MM"))
            {
                LoadPlan1();
                LoadResultList();
                dateTime1 = dtp1.DateTime;
            }
        }
        private void LoadPlan1()
        {
            if (tab_LPA.SelectedTabPageIndex == 0 && tab_LPA2.SelectedTabPageIndex == 1)
            {
                dt_Plan = new DataTable();
                bandedGridView_Plan.Columns.Clear();

                BandedGridColumn newColumn1 = new BandedGridColumn();
                BandedGridColumn newColumn2 = new BandedGridColumn();
                BandedGridColumn newColumn3 = new BandedGridColumn();
                newColumn1.FieldName = "ID";
                newColumn2.FieldName = "Name1";
                newColumn3.FieldName = "Scope";
                newColumn1.Name = "ID";
                newColumn2.Name = "Name1";
                newColumn3.Name = "Scope";
                newColumn1.Caption = "序号";
                newColumn2.Caption = "职位-姓名";
                newColumn3.Caption = "审核范围";
                newColumn1.Visible = true;
                newColumn2.Visible = true;
                newColumn3.Visible = true;
                newColumn1.Width = 15;
                newColumn2.Width = 75;
                newColumn3.Width = 35;
                newColumn1.MinWidth = 15;
                newColumn2.MinWidth = 75;
                newColumn3.MinWidth = 35;
                newColumn1.OwnerBand = bandedGridView_Plan.Bands["gridBandA0"]; // 关键：设置列的所属带区
                newColumn2.OwnerBand = bandedGridView_Plan.Bands["gridBandA0"]; // 关键：设置列的所属带区
                newColumn3.OwnerBand = bandedGridView_Plan.Bands["gridBandA0"]; // 关键：设置列的所属带区
                bandedGridView_Plan.Columns.Add(newColumn1);
                bandedGridView_Plan.Columns.Add(newColumn2);
                bandedGridView_Plan.Columns.Add(newColumn3);

                if (dtp4.Text != "")
                {
                    //创建datatable基础表，用于后面往里写内容
                    dt_Plan.Columns.Add("ID", typeof(int));
                    dt_Plan.Columns.Add("Name1", typeof(string));
                    dt_Plan.Columns.Add("Scope", typeof(string));
                    for (int i = 1; i < 32; i++)
                    {
                        DataColumn dataColumn = new DataColumn();
                        dataColumn.ColumnName = "b" + i.ToString();
                        dataColumn.Caption = "b" + i.ToString();
                        dataColumn.DataType = typeof(string);
                        dt_Plan.Columns.Add(dataColumn);
                    }

                    gridControl_Plan.DataSource = dt_Plan;

                    DateTime dateTime1 = Convert.ToDateTime(dtp1.DateTime.ToString("yyyy/MM/01"));
                    DateTime dateTime2 = Convert.ToDateTime(dtp1.DateTime.ToString("yyyy/MM/01")).AddMonths(1);
                    DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "View_LPA_Plan", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date>='" + dateTime1 + "' and Date<'" + dateTime2 + "' and Del_tag=0");

                    List<string> list = new List<string>();
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        // ① 第一步，循环添加列名
                        //获取当月的日期
                        int day = Convert.ToDateTime(dt1.Rows[i]["Date"].ToString()).Day;
                        //获取日期的实例Band，获取它的列数，0列说明没有内容需要添加，大于0说明有内容了，不用添加了
                        GridBand targetBand = bandedGridView_Plan.Bands["gridBandA" + day];

                        if (targetBand.Columns.Count == 0)
                        {
                            RepositoryItemMemoEdit memoEdit = new RepositoryItemMemoEdit();
                            // 1. 创建新列
                            BandedGridColumn newColumn = new BandedGridColumn();
                            newColumn.FieldName = "b" + day; // 绑定数据源的字段名  注意：必须与上面dt表的列名一致，否则无法添加入数据
                            newColumn.Caption = dt1.Rows[i]["Scope"].ToString();          // 显示标题
                            newColumn.Visible = true;
                            newColumn.ColumnEdit = memoEdit;

                            //创建datatable基础表，用于后面往里写内容
                            //dt.Columns.Add(dt1.Rows[i]["Scope"].ToString(), typeof(string));

                            // 3. 将列添加到带区
                            if (targetBand != null)
                            {
                                newColumn.OwnerBand = targetBand; // 关键：设置列的所属带区
                                bandedGridView_Plan.Columns.Add(newColumn);
                            }
                        }
                        // ② 第二步，循环添加行标题
                        if (!list.Contains(dt1.Rows[i]["FullName"].ToString()))
                        {
                            list.Add(dt1.Rows[i]["FullName"].ToString());
                            dt_Plan.Rows.Add(list.Count, dt1.Rows[i]["EmployeeTitle"].ToString() + "-" + dt1.Rows[i]["FullName"].ToString());
                        }
                    }

                    // ③ 第三步，循环添加行内容
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        int day = Convert.ToDateTime(dt1.Rows[i]["Date"].ToString()).Day;
                        int rowindex = list.IndexOf(dt1.Rows[i]["FullName"].ToString());
                        dt_Plan.Rows[rowindex]["b" + day] = dt1.Rows[i]["Type"].ToString();
                    }
                }

                // 循环设置列宽和列标题栏背景色
                foreach (GridBand gridBand in bandedGridView_Plan.Bands)
                {
                    gridBand.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                    gridBand.AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                    gridBand.AppearanceHeader.ForeColor = Color.White;
                    if (gridBand.Caption != "日期")
                    {
                        int a = (gridControl_Plan.Width - bandedGridView_Plan.Bands[0].Width) / 31;
                        gridBand.Width = a;
                    }
                    gridBand.AppearanceHeader.Font = new Font("Tahoma", 9, FontStyle.Bold);
                }
            }
        }
        //计划表的背景色变更
        DataTable dt_list = new DataTable();
        private void bandedGridView_Plan_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            // 只处理从第3列开始的列
            if (e.Column.AbsoluteIndex >= 3)
            {
                // 获取当前单元格的值
                string val = e.CellValue?.ToString();

                if (val != "")
                {
                    GridColumn col = bandedGridView_Plan.Columns["Name1"];
                    string Auditor = bandedGridView_Plan.GetRowCellValue(e.RowHandle, col).ToString();
                    DateTime dateTime = new DateTime(dtp1.DateTime.Year, dtp1.DateTime.Month, Convert.ToInt32(e.Column.FieldName.Substring(1)));

                    DataRow[] dr1 = dt_list.Select("Auditor='" + Auditor + "' and Date='" + dateTime + "' and Del_tag=0");
                    if (dr1.Length > 0)
                    {
                        DataRow[] dr2 = dt_list.Select("Auditor='" + Auditor + "' and Date='" + dateTime + "' and Del_tag=0 and Problem=1");
                        if (dr2.Length > 0)
                        {
                            e.Appearance.BackColor = Color.Red;
                        }
                        else
                        {
                            DataRow[] dr3 = dt_list.Select("Auditor='" + Auditor + "' and Date='" + dateTime + "' and Del_tag=0 and Problem=2");
                            if (dr3.Length > 0)
                            {
                                e.Appearance.BackColor = Color.Yellow;
                            }
                            else
                            {
                                e.Appearance.BackColor = Color.Lime;
                            }
                        }
                    }
                    else
                    {
                        e.Appearance.BackColor = Color.FromArgb(224, 224, 224);
                    }
                }
            }
        }
        #endregion

        #region Member information      
        public void LoadMember()
        {
            gridView_Member.OptionsView.ShowIndicator = false;

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("No.", typeof(int));
            dt1.Columns.Add("Name", typeof(string));
            dt1.Columns.Add("Position", typeof(string));
            dt1.Columns.Add("ID", typeof(int));
            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_User", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                dt1.Rows.Add(i + 1, dt2.Rows[i][4].ToString(), dt2.Rows[i][5].ToString(), dt2.Rows[i][0].ToString());
                string displayName = dt2.Rows[i]["EmployeeTitle"].ToString() + "-" + dt2.Rows[i]["FullName"].ToString();
                string tagValue = dt2.Rows[i][0].ToString();
                cmb_Name4.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
            gridControl_Member.DataSource = dt1;
            gridView_Member.Columns[3].Visible = false;

            gridView_Member.Appearance.Row.Font = new Font("Tahoma", 12);
            gridView_Member.Appearance.HeaderPanel.Font = new Font("Tahoma", 12, FontStyle.Bold);

            for (int i = 0; i < gridView_Member.Columns.Count; i++)
            {
                gridView_Member.Columns[i].AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                gridView_Member.Columns[i].AppearanceHeader.ForeColor = Color.White;
                gridView_Member.Columns[i].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridView_Member.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
            gridView_Member.Columns[0].Width = 50;

            gridView_Member.OptionsView.EnableAppearanceEvenRow = true;
            gridView_Member.OptionsView.EnableAppearanceOddRow = true;
            gridView_Member.Appearance.OddRow.BackColor = Color.FromArgb(242, 242, 242);  //奇数行背景颜色
            gridView_Member.Appearance.EvenRow.BackColor = Color.FromArgb(217, 217, 217);  //偶数行背景颜色
        }
        private void btn_Add2_Click(object sender, EventArgs e)
        {
            Form_LPA_Member frm = new Form_LPA_Member();
            frm.labelControl3.Text = " Add member information";
            frm.LoadName();
            frm.FormType = "Add";
            frm.ShowDialog();
        }
        private void btn_Revise2_Click(object sender, EventArgs e)
        {
            if (gridView_Member.RowCount > 0)
            {
                object value1 = gridView_Member.GetRowCellValue(gridView_Member.FocusedRowHandle, gridView_Member.Columns[1]);
                object value2 = gridView_Member.GetRowCellValue(gridView_Member.FocusedRowHandle, gridView_Member.Columns[2]);
                object value3 = gridView_Member.GetRowCellValue(gridView_Member.FocusedRowHandle, gridView_Member.Columns[3]);
                Form_LPA_Member frm = new Form_LPA_Member();
                frm.labelControl3.Text = " Revise member information";
                frm.LoadName();
                frm.FormType = "Revise";
                frm.ID = Convert.ToInt32(value3);
                foreach (object item in frm.cmb_Name.Properties.Items)
                {
                    if (item.ToString() == value1.ToString())
                    {
                        frm.cmb_Name.SelectedIndex = frm.cmb_Name.Properties.Items.IndexOf(item);
                    }
                }
                frm.txt_Position.Text = value2.ToString();
                frm.ShowDialog();
            }
        }
        private void btn_Delete2_Click(object sender, EventArgs e)
        {
            if (gridView_Member.RowCount > 0)
            {
                object value1 = gridView_Member.GetRowCellValue(gridView_Member.FocusedRowHandle, gridView_Member.Columns[1]);
                object value2 = gridView_Member.GetRowCellValue(gridView_Member.FocusedRowHandle, gridView_Member.Columns[2]);
                object value3 = gridView_Member.GetRowCellValue(gridView_Member.FocusedRowHandle, gridView_Member.Columns[3]);
                Form_LPA_Member frm = new Form_LPA_Member();
                frm.labelControl3.Text = " Delete member information";
                frm.navigationFrame1.SelectedPageIndex = 1;
                frm.labelControl5.Text = value1 + "    " + value2;
                frm.ID = Convert.ToInt32(value3);
                frm.ShowDialog();
            }
        }
        private void btn_MemberBack_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
        }
        #endregion

        #region Audit scope
        public void LoadAuditScope()
        {
            gridView_Audit.OptionsView.ShowIndicator = false;

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("No.", typeof(int));
            dt1.Columns.Add("Audit scope", typeof(string));
            dt1.Columns.Add("ID", typeof(int));
            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_AuditScope", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                dt1.Rows.Add(i + 1, dt2.Rows[i][3].ToString(), dt2.Rows[i][0].ToString());
                string displayName = dt2.Rows[i]["Scope"].ToString();
                string tagValue = dt2.Rows[i][0].ToString();
                cmb_Scope4.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
            gridControl_Audit.DataSource = dt1;
            gridView_Audit.Columns[2].Visible = false;

            gridView_Audit.Appearance.Row.Font = new Font("Tahoma", 12);
            gridView_Audit.Appearance.HeaderPanel.Font = new Font("Tahoma", 12, FontStyle.Bold);

            for (int i = 0; i < gridView_Audit.Columns.Count; i++)
            {
                gridView_Audit.Columns[i].AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                gridView_Audit.Columns[i].AppearanceHeader.ForeColor = Color.White;
                gridView_Audit.Columns[i].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridView_Audit.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
            gridView_Audit.Columns[0].Width = 25;

            gridView_Audit.OptionsView.EnableAppearanceEvenRow = true;
            gridView_Audit.OptionsView.EnableAppearanceOddRow = true;
            gridView_Audit.Appearance.OddRow.BackColor = Color.FromArgb(242, 242, 242);  //奇数行背景颜色
            gridView_Audit.Appearance.EvenRow.BackColor = Color.FromArgb(217, 217, 217);  //偶数行背景颜色
        }
        private void btn_Add3_Click(object sender, EventArgs e)
        {
            Form_LPA_Audit frm = new Form_LPA_Audit();
            frm.labelControl3.Text = " Add audit scope";
            frm.FormType = "Add";
            frm.ShowDialog();
        }
        private void btn_Revise3_Click(object sender, EventArgs e)
        {
            if (gridView_Audit.RowCount > 0)
            {
                object value2 = gridView_Audit.GetRowCellValue(gridView_Audit.FocusedRowHandle, gridView_Audit.Columns[1]);
                object value3 = gridView_Audit.GetRowCellValue(gridView_Audit.FocusedRowHandle, gridView_Audit.Columns[2]);
                Form_LPA_Audit frm = new Form_LPA_Audit();
                frm.labelControl3.Text = " Revise audit scope";
                frm.FormType = "Revise";
                frm.ID = Convert.ToInt32(value3);
                frm.txt_Scope.Text = value2.ToString();
                frm.ShowDialog();
            }
        }
        private void btn_Delete3_Click(object sender, EventArgs e)
        {
            if (gridView_Audit.RowCount > 0)
            {
                object value2 = gridView_Audit.GetRowCellValue(gridView_Audit.FocusedRowHandle, gridView_Audit.Columns[1]);
                object value3 = gridView_Audit.GetRowCellValue(gridView_Audit.FocusedRowHandle, gridView_Audit.Columns[2]);
                Form_LPA_Audit frm = new Form_LPA_Audit();
                frm.labelControl3.Text = " Delete audit scope";
                frm.navigationFrame1.SelectedPageIndex = 1;
                frm.labelControl5.Text = value2.ToString();
                frm.ID = Convert.ToInt32(value3);
                frm.ShowDialog();
            }
        }
        private void btn_AuditBack_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
        }
        #endregion

        #region Tcard
        private void LoadItem()
        {
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard0_Item", "Del_tag=0");
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string displayName = dt1.Rows[i]["Item"].ToString();
                string tagValue = dt1.Rows[i][0].ToString();
                cmb_Item5.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
        }
        //加载左侧按钮及按钮的点击事件
        private void LoadTcardType()
        {
            panelControl7.Controls.Clear();

            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard1_Type", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                SimpleButton btn = new SimpleButton();
                btn.Text = dt1.Rows[i][2].ToString();
                btn.Tag = dt1.Rows[i][0].ToString();
                btn.Size = new Size(130, 60);
                btn.Location = new Point((i % 3) * 140 + 30, (i / 3) * 70 + 30);
                btn.Font = new Font("宋体", 12);
                btn.Appearance.BackColor = Color.FromArgb(217, 217, 217);
                btn.Click += (s, e) =>
                {
                    foreach (SimpleButton control in panelControl7.Controls)
                    {
                        control.Appearance.BackColor = Color.FromArgb(217, 217, 217);
                    }
                    btn.Appearance.BackColor = Color.FromArgb(30, 42, 99);
                    tab_Tcard.Visible = true;
                    tab_Tcard.SelectedTabPageIndex = 0;
                    labelControl18.Text = btn.Text;
                    labelControl18.Tag = btn.Tag.ToString();
                    LoadTcardTypeView(btn.Text, btn.Tag);
                };
                panelControl7.Controls.Add(btn);
            }
        }
        public void LoadTcardTypeView(string Tp, object tag)
        {
            gridView_Tcard.OptionsView.ShowIndicator = false;

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("No.", typeof(int));
            dt1.Columns.Add("Type", typeof(string));
            dt1.Columns.Add("Problem Description", typeof(string));
            dt1.Columns.Add("Comments", typeof(string));
            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard2_Problem", "Tcard_Type_ID=" + tag + " and Del_tag=0");
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                dt1.Rows.Add(i + 1, Tp, dt2.Rows[i][2].ToString(), dt2.Rows[i][3].ToString());
            }
            gridControl_Tcard.DataSource = dt1;

            gridView_Tcard.Appearance.Row.Font = new Font("Tahoma", 12);
            gridView_Tcard.Appearance.HeaderPanel.Font = new Font("Tahoma", 12, FontStyle.Bold);

            for (int i = 0; i < gridView_Tcard.Columns.Count; i++)
            {
                gridView_Tcard.Columns[i].AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                gridView_Tcard.Columns[i].AppearanceHeader.ForeColor = Color.White;
                gridView_Tcard.Columns[i].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridView_Tcard.Columns[i].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
            gridView_Tcard.Columns[0].Width = 100;
            gridView_Tcard.Columns[1].Width = 200;

            gridView_Tcard.OptionsView.EnableAppearanceEvenRow = true;
            gridView_Tcard.OptionsView.EnableAppearanceOddRow = true;
            gridView_Tcard.Appearance.OddRow.BackColor = Color.FromArgb(242, 242, 242);  //奇数行背景颜色
            gridView_Tcard.Appearance.EvenRow.BackColor = Color.FromArgb(217, 217, 217);  //偶数行背景颜色
        }
        private void btn_Add5_Click(object sender, EventArgs e)
        {
            labelControl5.Text = " Add T-card information";
            tab_Tcard.SelectedTabPageIndex = 1;
            tab_Tcard.Visible = true;
            panel6.Controls.Clear();
            txt_Scope5.Text = "";
            lb_Repeated.Visible = false;
            txt_Scope5.Focus();
            cmb_Item5.SelectedIndex = -1;
        }
        private void btn_Revise5_Click(object sender, EventArgs e)
        {
            lb_Repeated.Visible = false;
            if (labelControl18.Text != "" && labelControl18.Tag != null)
            {
                tab_Tcard.SelectedTabPageIndex = 1;
                tab_Tcard.Visible = true;
                panel6.Controls.Clear();
                txt_Scope5.Text = labelControl18.Text;
                labelControl5.Text = " Revise T-card information";
                LoadProblemList();
            }
        }
        private void LoadProblemList()
        {
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard1_Type", "ID=" + labelControl18.Tag + " and Del_tag=0");
            if (dt1.Rows.Count > 0)
            {
                cmb_Item5.SelectedIndex = FindComboBoxItemIndex_Tag(cmb_Item5, dt1.Rows[0]["Tcard_Item_ID"].ToString());
            }

            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard2_Problem", "Tcard_Type_ID=" + labelControl18.Tag + " and Del_tag=0");
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                AddProblem(dt2.Rows[i]["Problems"].ToString(), dt2.Rows[i]["Comments"].ToString());
            }
        }
        private void btn_TcardBack_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
        }
        //右侧增加
        private void btn_Add51_Click(object sender, EventArgs e)
        {
            AddProblem("", "");
        }
        private void AddProblem(string Problem, string Comment)
        {
            if (lb_Repeated.Visible == false)
            {
                panel6.AutoScrollPosition = new Point(0, 0);
                int a = panel6.Controls.Count;//实际数量
                int b = 0;
                foreach (Control control in panel6.Controls)
                {
                    if (control.Visible == true)
                    {
                        b++;
                    }
                }

                Panel panel = new Panel
                {
                    Size = new Size(1005, 50),
                    Location = new Point(0, (b * 60)),
                    Name = "panel" + a,
                    Tag = a,
                };

                LabelControl labelControl = new LabelControl
                {
                    Parent = panel,
                    Font = new Font("Tahoma", 14),
                    Location = new Point(10, 7),
                    Text = (a + 1).ToString() + "."
                };

                TextEdit textEdit1 = new TextEdit
                {
                    Parent = panel,
                    BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder,
                    Font = new Font("Tahoma", 12),
                    Width = 445,
                    Location = new Point(40, 1),
                    BackColor = Color.Transparent,
                    Name = "txt1" + a,
                    Text = Problem
                };

                TextEdit textEdit2 = new TextEdit
                {
                    Parent = panel,
                    BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder,
                    Font = new Font("Tahoma", 12),
                    Width = 445,
                    Location = new Point(525, 1),
                    BackColor = Color.Transparent,
                    Name = "txt2" + a,
                    Text = Comment
                };

                SeparatorControl separatorControl1 = new SeparatorControl
                {
                    Parent = panel,
                    Size = new Size(449, 20),
                    BackColor = Color.Transparent,
                    Location = new Point(36, 30)
                };

                SeparatorControl separatorControl2 = new SeparatorControl
                {
                    Parent = panel,
                    Size = new Size(449, 20),
                    BackColor = Color.Transparent,
                    Location = new Point(521, 30)
                };

                SimpleButton deleteButton = new SimpleButton
                {
                    Text = "",
                    Parent = panel,
                    Size = new Size(30, 30),
                    BackColor = Color.Transparent,
                    Location = new Point(970, 10)
                };
                deleteButton.ImageOptions.SvgImage = SvgImage.FromResources("LPA.Resources.delete1.svg", Assembly.GetExecutingAssembly());
                deleteButton.ImageOptions.SvgImageSize = new Size(25, 25);
                deleteButton.ImageOptions.ImageToTextAlignment = ImageAlignToText.TopCenter;
                deleteButton.ImageOptions.ImageToTextIndent = 0;
                deleteButton.Click += DeleteButton_Click;

                panel.Controls.Add(textEdit1);
                panel.Controls.Add(textEdit2);
                panel.Controls.Add(separatorControl1);
                panel.Controls.Add(separatorControl2);
                panel.Controls.Add(deleteButton);

                panel6.Controls.Add(panel);
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            // 获取按钮所在的 Panel
            if (sender is SimpleButton btn && btn.Parent is Panel panel)
            {
                // 获取 Panel 的父容器（如 Form 或 GroupControl）
                Control parentContainer = panel.Parent;

                panel.Visible = false;

                int id = Convert.ToInt32(panel.Name.Substring(5));

                foreach (Control child in panel6.Controls)
                {
                    if (Convert.ToInt32(child.Name.Substring(5)) > id)
                    {
                        child.Top -= 60;
                    }
                }
            }
        }
        private void txt_Scope_Leave(object sender, EventArgs e)
        {
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard1_Type", "Type='" + txt_Scope5.Text + "' and AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            if (dt1.Rows.Count > 0)
            {
                lb_Repeated.Visible = true;
                txt_Scope5.Focus();
            }
            else
            {
                lb_Repeated.Visible = false;
            }
        }
        //右侧Problem保存事件
        private void btn_Save5_Click(object sender, EventArgs e)
        {
            if (cmb_Item5.Text != "")
            {
                if (txt_Scope5.Text != "")
                {
                    if (lb_Repeated.Visible == false)
                    {
                        var selectedItem = cmb_Item5.SelectedItem as ComboBoxItem;
                        if (selectedItem != null)
                        {
                            var itemID = selectedItem.Tag; // 这里可以使用tag
                            if (labelControl5.Text == " Add T-card information")
                            {
                                string Item = cmb_Item5.Text;
                                string Type = txt_Scope5.Text;
                                int AuditFactortyID = SystemInfo.readFactory;
                                int AuditDepartmentID = SystemInfo.readDepartment;
                                int CreatorID = SystemInfo.userID;
                                string CreationDate = DateTime.Now.ToString();
                                int a = SQL.InsertReturnID(SystemInfo.SQL, "LPA_Tcard1_Type", "Tcard_Item_ID,Type,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate", $"{itemID},'{Type}',{AuditFactortyID},{AuditDepartmentID},{CreatorID},'{CreationDate}'");
                                if (a > 0)
                                {
                                    int b = panel6.Controls.Count;
                                    for (int i = 0; i < b; i++)
                                    {
                                        if (panel6.Controls[i].Visible == true)
                                        {
                                            string txt1 = panel6.Controls[i].Controls["txt1" + i].Text;
                                            string txt2 = panel6.Controls[i].Controls["txt2" + i].Text;
                                            SQL.Insert(SystemInfo.SQL, "LPA_Tcard2_Problem", "Tcard_Item_ID,Tcard_Type_ID,Problems,Comments,CreatorID,CreationDate", $"{itemID},{a},'{txt1}','{txt2}',{SystemInfo.userID},'{DateTime.Now}'");
                                        }
                                    }
                                    tab_Tcard.Visible = false;
                                    LoadTcardType();
                                }
                                else
                                {
                                    DevExpress.XtraEditors.XtraMessageBox.Show("Insert failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }

                            }
                            else if (labelControl5.Text == " Revise T-card information")
                            {
                                var selectedItem2 = cmb_Item5.SelectedItem as ComboBoxItem;
                                if (selectedItem2 != null)
                                {
                                    var itemTag = selectedItem2.Tag; // 这里可以使用tag
                                    //修改内容，第一步先修改LPA_Tcard1_Type的表
                                    SQL.Update(SystemInfo.SQL, "LPA_Tcard1_Type", $"{"Tcard_Item_ID = " + itemTag},{"Type = '" + txt_Scope5.Text + "'"} ", "ID=" + labelControl18.Tag);

                                    //修改内容，第二步先修改LPA_Tcard2_Problem的表
                                    SQL.Update(SystemInfo.SQL, "LPA_Tcard2_Problem", "Del_tag=1", "Tcard_Type_ID=" + labelControl18.Tag);
                                    int b = panel6.Controls.Count;
                                    for (int i = 0; i < b; i++)
                                    {
                                        if (panel6.Controls[i].Visible == true)
                                        {
                                            string txt1 = panel6.Controls[i].Controls["txt1" + i].Text;
                                            string txt2 = panel6.Controls[i].Controls["txt2" + i].Text;
                                            SQL.Insert(SystemInfo.SQL, "LPA_Tcard2_Problem", "Tcard_Type_ID,Problems,Comments,CreatorID,CreationDate", $"{labelControl18.Tag},'{txt1}','{txt2}',{SystemInfo.userID},'{DateTime.Now}'");
                                        }
                                    }
                                    tab_Tcard.Visible = false;
                                    LoadTcardType();
                                }
                                else
                                {
                                    DevExpress.XtraEditors.XtraMessageBox.Show("Unknown error\r\nError code: 104", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                        }
                        else
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("Unknown error\r\nError code: 103", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("The Tcard type is repeated!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txt_Scope5.Focus();
                    }
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the Tcard type!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_Scope5.Focus();
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Please select the Tcard item!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btn_Cancel5_Click(object sender, EventArgs e)
        {
            tab_Tcard.Visible = false;
        }
        private void btn_Delete5_Click(object sender, EventArgs e)
        {
            if (labelControl18.Text != "" && labelControl18.Tag != null)
            {
                DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure to delete this Tcard type?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                    SQL.Update(SystemInfo.SQL, "LPA_Tcard1_Type", "Del_tag=1", "ID=" + labelControl18.Tag);
                    SQL.Update(SystemInfo.SQL, "LPA_Tcard2_Problem", "Del_tag=1", "Tcard_Type_ID=" + labelControl18.Tag);
                    tab_Tcard.Visible = false;
                    LoadTcardType();
                    labelControl18.Text = "";
                    labelControl18.Tag = "";
                }
            }
        }
        #endregion

        #region Make LPA plan      
        private void LoadBasicInfo()
        {
            cmb_Name4.Properties.Items.Clear();
            cmb_Scope4.Properties.Items.Clear();
            cmb_Tcard4.Properties.Items.Clear();
            //加载人员信息
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_User", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string displayName = dt1.Rows[i]["EmployeeTitle"].ToString() + "-" + dt1.Rows[i]["FullName"].ToString();
                string tagValue = dt1.Rows[i][0].ToString();
                cmb_Name4.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
            //加载Scope
            DataTable dt2 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_AuditScope", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                string displayName = dt2.Rows[i]["Scope"].ToString();
                string tagValue = dt2.Rows[i][0].ToString();
                cmb_Scope4.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
            //加载Tcard type
            DataTable dt3 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Tcard1_Type", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt3.Rows.Count; i++)
            {
                string displayName = dt3.Rows[i]["Type"].ToString();
                string tagValue = dt3.Rows[i][0].ToString();
                cmb_Tcard4.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
        }
        private void btn_PlanBack_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
        }
        private void btn_Apply4_Click(object sender, EventArgs e)
        {
            if (dtp4.Text != "")
            {
                if (cmb_Name4.Text != "")
                {
                    if (cmb_Scope4.Text != "")
                    {
                        if (cmb_Tcard4.Text != "")
                        {
                            var Name4 = cmb_Name4.SelectedItem as ComboBoxItem;
                            var Scope4 = cmb_Scope4.SelectedItem as ComboBoxItem;
                            var Tcard4 = cmb_Tcard4.SelectedItem as ComboBoxItem;
                            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_Plan", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date='" + dtp4.DateTime.Date + "' and Del_tag=0");

                            //限制：如果日期当天有没有内容，没有直接添加，有就判断是不是跟现在是一个Scope，不是一个报错
                            if (dt1.Rows.Count == 0)
                            {
                                SQL.InsertReturnID(SystemInfo.SQL, "LPA_Plan", "AuditFactortyID,AuditDepartmentID,Date,NameID,ScopeID,Tcard_Type_ID,CreatorID,CreationDate", $"{SystemInfo.readFactory},{SystemInfo.readDepartment},'{dtp4.DateTime.Date}',{Name4.Tag},{Scope4.Tag},{Tcard4.Tag},{SystemInfo.userID},'{DateTime.Now}'");
                            }
                            else
                            {
                                if (dt1.Rows[0]["ScopeID"].ToString() == Scope4.Tag.ToString())
                                {
                                    DataRow[] dr1 = dt1.Select("NameID=" + Name4.Tag);
                                    if (dr1.Length == 0)
                                    {
                                        SQL.InsertReturnID(SystemInfo.SQL, "LPA_Plan", "AuditFactortyID,AuditDepartmentID,Date,NameID,ScopeID,Tcard_Type_ID,CreatorID,CreationDate", $"{SystemInfo.readFactory},{SystemInfo.readDepartment},'{dtp4.DateTime.Date}',{Name4.Tag},{Scope4.Tag},{Tcard4.Tag},{SystemInfo.userID},'{DateTime.Now}'");
                                    }
                                    else
                                    {
                                        DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show("The person already has an arrangement on the " + dtp4.DateTime.ToString("yyyy/MM/dd") + ", should it be replaced?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                                        if (dr == DialogResult.Yes)
                                        {
                                            SQL.Update(SystemInfo.SQL, "LPA_Plan", "Tcard_Type_ID=" + Tcard4.Tag, "ID=" + dr1[0][0].ToString());
                                        }
                                    }
                                }
                                else
                                {
                                    DevExpress.XtraEditors.XtraMessageBox.Show("Scope inconsistency!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            LoadPlan2();
                        }
                        else
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the T-card!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the Scope!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the Position-Name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the Date!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        DataTable dt_MakePlan;
        DateTime dateTime4;
        private void dtp4_EditValueChanged(object sender, EventArgs e)
        {
            if (dtp4.DateTime.ToString("yyyy/MM") != dateTime4.ToString("yyyy/MM"))
            {
                LoadPlan2();
                dateTime4 = dtp4.DateTime;
            }
        }
        private void LoadPlan2()
        {
            dt_MakePlan = new DataTable();
            bandedGridView_MakePlan.Columns.Clear();

            BandedGridColumn newColumn1 = new BandedGridColumn();
            BandedGridColumn newColumn2 = new BandedGridColumn();
            BandedGridColumn newColumn3 = new BandedGridColumn();
            BandedGridColumn newColumn4 = new BandedGridColumn();
            newColumn1.FieldName = "ID";
            newColumn2.FieldName = "Name1";
            newColumn3.FieldName = "Scope";
            newColumn4.FieldName = "UserID";
            newColumn1.Name = "ID";
            newColumn2.Name = "Name1";
            newColumn3.Name = "Scope";
            newColumn4.Name = "UserID";
            newColumn1.Caption = "序号";
            newColumn2.Caption = "职位-姓名";
            newColumn3.Caption = "审核范围";
            newColumn4.Caption = "UserID";
            newColumn1.Visible = true;
            newColumn2.Visible = true;
            newColumn3.Visible = true;
            newColumn4.Visible = false;
            newColumn1.Width = 15;
            newColumn2.Width = 75;
            newColumn3.Width = 35;
            newColumn1.MinWidth = 15;
            newColumn2.MinWidth = 75;
            newColumn3.MinWidth = 35;
            newColumn1.OwnerBand = bandedGridView_MakePlan.Bands["gridBandB0"]; // 关键：设置列的所属带区
            newColumn2.OwnerBand = bandedGridView_MakePlan.Bands["gridBandB0"]; // 关键：设置列的所属带区
            newColumn3.OwnerBand = bandedGridView_MakePlan.Bands["gridBandB0"]; // 关键：设置列的所属带区
            newColumn4.OwnerBand = bandedGridView_MakePlan.Bands["gridBandB0"]; // 关键：设置列的所属带区
            bandedGridView_MakePlan.Columns.Add(newColumn1);
            bandedGridView_MakePlan.Columns.Add(newColumn2);
            bandedGridView_MakePlan.Columns.Add(newColumn3);
            bandedGridView_MakePlan.Columns.Add(newColumn4);

            if (dtp4.Text != "")
            {
                //创建datatable基础表，用于后面往里写内容
                dt_MakePlan.Columns.Add("ID", typeof(int));
                dt_MakePlan.Columns.Add("Name1", typeof(string));
                dt_MakePlan.Columns.Add("Scope", typeof(string));
                dt_MakePlan.Columns.Add("UserID", typeof(int));
                for (int i = 1; i < 32; i++)
                {
                    DataColumn dataColumn = new DataColumn();
                    dataColumn.ColumnName = "b" + i.ToString();
                    dataColumn.Caption = "b" + i.ToString();
                    dataColumn.DataType = typeof(string);
                    dt_MakePlan.Columns.Add(dataColumn);
                }

                gridControl_MakePlan.DataSource = dt_MakePlan;

                DateTime dateTime1 = Convert.ToDateTime(dtp4.DateTime.ToString("yyyy/MM/01"));
                DateTime dateTime2 = Convert.ToDateTime(dtp4.DateTime.ToString("yyyy/MM/01")).AddMonths(1);
                DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "View_LPA_Plan", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date>='" + dateTime1 + "' and Date<'" + dateTime2 + "' and Del_tag=0");

                List<string> list = new List<string>();
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    // ① 第一步，循环添加列名
                    //获取当月的日期
                    int day = Convert.ToDateTime(dt1.Rows[i]["Date"].ToString()).Day;
                    //获取日期的实例Band，获取它的列数，0列说明没有内容需要添加，大于0说明有内容了，不用添加了
                    GridBand targetBand = bandedGridView_MakePlan.Bands["gridBandB" + day];

                    if (targetBand.Columns.Count == 0)
                    {
                        RepositoryItemMemoEdit memoEdit = new RepositoryItemMemoEdit();
                        // 1. 创建新列
                        BandedGridColumn newColumn = new BandedGridColumn();
                        newColumn.FieldName = "b" + day; // 绑定数据源的字段名  注意：必须与上面dt表的列名一致，否则无法添加入数据
                        newColumn.Caption = dt1.Rows[i]["Scope"].ToString();          // 显示标题
                        newColumn.Visible = true;
                        newColumn.ColumnEdit = memoEdit;

                        //创建datatable基础表，用于后面往里写内容
                        //dt.Columns.Add(dt1.Rows[i]["Scope"].ToString(), typeof(string));

                        // 3. 将列添加到带区
                        if (targetBand != null)
                        {
                            newColumn.OwnerBand = targetBand; // 关键：设置列的所属带区
                            bandedGridView_MakePlan.Columns.Add(newColumn);
                        }
                    }
                    // ② 第二步，循环添加行标题
                    if (!list.Contains(dt1.Rows[i]["FullName"].ToString()))
                    {
                        list.Add(dt1.Rows[i]["FullName"].ToString());
                        dt_MakePlan.Rows.Add(list.Count, dt1.Rows[i]["EmployeeTitle"].ToString() + "-" + dt1.Rows[i]["FullName"].ToString(), "", dt1.Rows[i]["NameID"].ToString());
                    }
                }

                // ③ 第三步，循环添加行内容
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    int day = Convert.ToDateTime(dt1.Rows[i]["Date"].ToString()).Day;
                    int rowindex = list.IndexOf(dt1.Rows[i]["FullName"].ToString());
                    dt_MakePlan.Rows[rowindex]["b" + day] = dt1.Rows[i]["Type"].ToString();
                }
            }

            // 循环设置列宽
            foreach (GridBand gridBand in bandedGridView_MakePlan.Bands)
            {
                gridBand.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                gridBand.AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                gridBand.AppearanceHeader.ForeColor = Color.White;
                if (gridBand.Caption != "日期")
                {
                    int a = (gridControl_MakePlan.Width - bandedGridView_MakePlan.Bands[0].Width) / 31;
                    gridBand.Width = a;
                }
                gridBand.AppearanceHeader.Font = new Font("Tahoma", 9, FontStyle.Bold);
            }
        }
        private void bandedGridView_MakePlan_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            if (e.HitInfo.Column != null)
            {
                if (e.HitInfo.Column.AbsoluteIndex > 2)
                {
                    if (e.HitInfo.InRow)
                    {
                        // 显示PopupMenu并标记事件已处理
                        popupMenu1.ShowPopup(Control.MousePosition);
                    }
                }
            }
        }
        private void barButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int focusedRowHandle = bandedGridView_MakePlan.FocusedRowHandle;
            string focusedColumnHandle = bandedGridView_MakePlan.FocusedColumn.FieldName;

            string PositionName = bandedGridView_MakePlan.GetRowCellValue(focusedRowHandle, "Name1").ToString();
            string rowvalue = bandedGridView_MakePlan.GetRowCellValue(focusedRowHandle, focusedColumnHandle).ToString();
            DateTime dateTime = new DateTime(dtp4.DateTime.Year, dtp4.DateTime.Month, Convert.ToInt32(focusedColumnHandle.Substring(1)));
            string UserID = bandedGridView_MakePlan.GetRowCellValue(focusedRowHandle, "UserID").ToString();

            if (rowvalue != "")
            {
                DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure to delete the selected content?\r\n\r\nDate: " + dateTime.ToString("yyyy/MM/dd") + "\r\nPosition-Name: " + PositionName + "\r\nScope: " + bandedGridView_MakePlan.FocusedColumn.Caption + "\r\nT-card: " + rowvalue, "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dr == DialogResult.Yes)
                {
                    string Condition = "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date='" + dateTime + "' and NameID=" + UserID + " and Scope='" + bandedGridView_MakePlan.FocusedColumn.Caption + "' and Type='" + rowvalue + "'";
                    if (SQL.Update(SystemInfo.SQL, "View_LPA_Plan", "Del_tag=1", Condition) == true)
                    {
                        LoadPlan2();
                    }
                }
            }
        }
        #endregion

        #region 结果提交
        private void LoadBasicInfo6()
        {
            cmb_Auditor6.Properties.Items.Clear();
            cmb_Responsible6.Properties.Items.Clear();
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "LPA_User", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string displayName = dt1.Rows[i]["EmployeeTitle"].ToString() + "-" + dt1.Rows[i]["FullName"].ToString();
                string tagValue = dt1.Rows[i][0].ToString();
                cmb_Auditor6.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }

            DataTable dt5 = SQL.ReturnDataTable3(SystemInfo.SQL, "Employees", "FactoryID=" + SystemInfo.readFactory + "and  Del_tag=0");
            //将dataTable按定义排序
            dt5.DefaultView.Sort = "FullName asc";
            dt5 = dt5.DefaultView.ToTable();
            for (int i = 0; i < dt5.Rows.Count; i++)
            {
                string displayName = dt5.Rows[i]["FullName"].ToString() + " - " + dt5.Rows[i]["LoginName"].ToString();
                string tagValue = dt5.Rows[i][0].ToString();
                cmb_Responsible6.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
        }
        private void btn_EditBack_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
        }
        private void dtp61_EditValueChanged(object sender, EventArgs e)
        {
            FillScopeTcard();
        }
        private void cmb_Auditor6_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillScopeTcard();
        }
        private void FillScopeTcard()
        {
            txt_Scope6.Text = "";
            txt_Scope6.Tag = "";
            txt_Tcard6.Text = "";
            txt_Tcard6.Tag = "";
            if (dtp61.Text != "" && cmb_Auditor6.Text != "")
            {
                var selectedItem1 = cmb_Auditor6.SelectedItem as ComboBoxItem;
                if (selectedItem1 != null)
                {
                    var AuditorTag = selectedItem1.Tag;
                    string Condition = "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date='" + dtp61.DateTime + "' and NameID=" + AuditorTag + " and Del_tag=0";
                    DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "View_LPA_Plan", Condition);
                    if (dt1.Rows.Count > 0)
                    {
                        txt_Scope6.Text = dt1.Rows[0]["Scope"].ToString();
                        txt_Scope6.Tag = dt1.Rows[0]["ScopeID"].ToString();
                        txt_Tcard6.Text = dt1.Rows[0]["Type"].ToString();
                        txt_Tcard6.Tag = dt1.Rows[0]["Tcard_Type_ID"].ToString();
                    }
                }
            }
        }
        private void btn_Submit6_Click(object sender, EventArgs e)
        {
            if (dtp1.Text != "")
            {
                if (cmb_Auditor6.Text != "")
                {
                    if (txt_Scope6.Text != "" && txt_Tcard6.Text != "")
                    {
                        if (radioGroup1.SelectedIndex > -1)
                        {
                            if (radioGroup1.SelectedIndex == 1 && imageComboBoxEdit1.SelectedIndex == -1)
                            {
                                DevExpress.XtraEditors.XtraMessageBox.Show("Please select status!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            if (radioGroup1.SelectedIndex == 1 && dtp62.Text == "")
                            {
                                DevExpress.XtraEditors.XtraMessageBox.Show("Please select a scheduled completion date!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            if (radioGroup1.SelectedIndex == 1 && cmb_Responsible6.Text == "")
                            {
                                DevExpress.XtraEditors.XtraMessageBox.Show("Please select responsible!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            if (labelControl19.Text == "Submit the results")
                            {
                                InsertData();
                            }
                            else if (labelControl19.Text == "Update the results")
                            {
                                UpdateData();
                            }
                        }
                        else
                        {
                            DevExpress.XtraEditors.XtraMessageBox.Show("Please select whether the problem is found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("Please confirm the date or auditor information!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the auditor!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Please enter the date!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void InsertData()
        {
            var selectedItem1 = cmb_Auditor6.SelectedItem as ComboBoxItem;
            var selectedItem2 = cmb_Responsible6.SelectedItem as ComboBoxItem;
            int AuditorTag = -1;
            int ResponsibleTag = -1;

            if (selectedItem1 != null)
            {
                AuditorTag = Convert.ToInt32(selectedItem1.Tag);
            }
            if (selectedItem2 != null)
            {
                ResponsibleTag = Convert.ToInt32(selectedItem2.Tag);
            }

            string Date = dtp61.DateTime.ToString();
            string Auditor = cmb_Auditor6.Text;
            string Scope = txt_Scope6.Text;
            string Tcard = txt_Tcard6.Text;
            int Tcard_Type_ID = Convert.ToInt32(txt_Tcard6.Tag);
            int YesNo = Convert.ToInt32(radioGroup1.SelectedIndex);
            string ProblemDescription = txt_Description6.Text;
            string Responsible = cmb_Responsible6.Text;
            string ActionPlan = txt_ActionPlan6.Text;
            int Status;
            //没有问题时Status等于4(100%)，有问题时根据选择
            if (radioGroup1.SelectedIndex == 0)
            {
                Status = 4;
            }
            else
            {
                Status = imageComboBoxEdit1.SelectedIndex;
            }
            string Comments = txt_Comments6.Text;
            string Items;
            string Content;

            if (dtp62.Text != "")
            {
                string CompletionDate = dtp62.DateTime.ToString();
                Items = "Date,Auditor,Scope,Tcard,Problem,ProblemDescription,Responsible,ActionPlan,CompletionDate,Status,Comments,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate,AuditorID,ResponsibleID,Tcard_Type_ID";
                Content = $"'{Date}','{Auditor}','{Scope}','{Tcard}',{YesNo},'{ProblemDescription}','{Responsible}','{ActionPlan}','{CompletionDate}',{Status},'{Comments}',{SystemInfo.readFactory},{SystemInfo.readDepartment},{SystemInfo.userID},'{DateTime.Now}',{AuditorTag},{ResponsibleTag},{Tcard_Type_ID}";
            }
            else
            {
                Items = "Date,Auditor,Scope,Tcard,Problem,ProblemDescription,Responsible,ActionPlan,Status,Comments,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate,AuditorID,ResponsibleID,Tcard_Type_ID";
                Content = $"'{Date}','{Auditor}','{Scope}','{Tcard}',{YesNo},'{ProblemDescription}','{Responsible}','{ActionPlan}',{Status},'{Comments}',{SystemInfo.readFactory},{SystemInfo.readDepartment},{SystemInfo.userID},'{DateTime.Now}',{AuditorTag},{ResponsibleTag},{Tcard_Type_ID}";
            }
            int a = SQL.InsertReturnID(SystemInfo.SQL, "LPA_ResultList", Items, Content);

            if (a > 0)
            {
                //添加历史记录
                string type = "创建";
                int ListID = a;
                if (dtp62.Text != "")
                {
                    string CompletionDate = dtp62.DateTime.ToString();
                    string HistoryItems = "Type,ListID,Date,Auditor,Scope,Tcard,Problem,ProblemDescription,Responsible,ActionPlan,CompletionDate,Status,Comments,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate,AuditorID,ResponsibleID,Tcard_Type_ID";
                    string HistoryContent = $"'{type}',{a},'{Date}','{Auditor}','{Scope}','{Tcard}',{YesNo},'{ProblemDescription}','{Responsible}','{ActionPlan}','{CompletionDate}',{Status},'{Comments}',{SystemInfo.readFactory},{SystemInfo.readDepartment},{SystemInfo.userID},'{DateTime.Now}',{AuditorTag},{ResponsibleTag},{Tcard_Type_ID}";
                    SQL.Insert(SystemInfo.SQL, "LPA_ResultList_History", HistoryItems, HistoryContent);
                }
                else
                {
                    string HistoryItems = "Type,ListID,Date,Auditor,Scope,Tcard,Problem,ProblemDescription,Responsible,ActionPlan,Status,Comments,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate,AuditorID,ResponsibleID,Tcard_Type_ID";
                    string HistoryContent = $"'{type}',{a},'{Date}','{Auditor}','{Scope}','{Tcard}',{YesNo},'{ProblemDescription}','{Responsible}','{ActionPlan}',{Status},'{Comments}',{SystemInfo.readFactory},{SystemInfo.readDepartment},{SystemInfo.userID},'{DateTime.Now}',{AuditorTag},{ResponsibleTag},{Tcard_Type_ID}";
                    SQL.Insert(SystemInfo.SQL, "LPA_ResultList_History", HistoryItems, HistoryContent);
                }

                //清空内容
                tab_LPA.SelectedTabPageIndex = 0;
                LoadResultList();
                Empty6();
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void UpdateData()
        {
            if (lb_ListID.Text != "")
            {
                var selectedItem2 = cmb_Responsible6.SelectedItem as ComboBoxItem;
                int ResponsibleTag = -1;
                if (selectedItem2 != null)
                {
                    ResponsibleTag = Convert.ToInt32(selectedItem2.Tag);
                }

                string YesNo = "Problem=" + Convert.ToInt32(radioGroup1.SelectedIndex);
                string ProblemDescription = "ProblemDescription='" + txt_Description6.Text + "'";
                string Responsible = "Responsible='" + cmb_Responsible6.Text + "'";
                string ActionPlan = "ActionPlan='" + txt_ActionPlan6.Text + "'";
                string Status;
                //没有问题时Status等于4(100%)，有问题时根据选择
                if (radioGroup1.SelectedIndex == 0)
                {
                    Status = "Status=4";
                }
                else
                {
                    Status = "Status=" + imageComboBoxEdit1.SelectedIndex;
                }
                string Comments = "Comments='" + txt_Comments6.Text + "'";
                string ResponsibleID = "ResponsibleID=" + ResponsibleTag;
                string Content;
                if (dtp62.Text != "")
                {
                    Content = $"{YesNo},{ProblemDescription},{Responsible},{ActionPlan},{Status},{Comments},{ResponsibleID},{"CompletionDate='" + dtp62.DateTime + "'"}";
                }
                else
                {
                    Content = $"{YesNo},{ProblemDescription},{Responsible},{ActionPlan},{Status},{Comments},{ResponsibleID},{"CompletionDate=null"}";
                }
                if (SQL.Update(SystemInfo.SQL, "LPA_ResultList", Content, "ID=" + lb_ListID.Text) == true)
                {
                    //添加历史记录
                    string type = "修改";
                    int ListID = Convert.ToInt32(lb_ListID.Text);
                    var selectedItem1 = cmb_Auditor6.SelectedItem as ComboBoxItem;
                    int AuditorTag = -1;
                    if (selectedItem1 != null)
                    {
                        AuditorTag = Convert.ToInt32(selectedItem1.Tag);
                    }
                    string Date = dtp61.DateTime.ToString();
                    string Auditor = cmb_Auditor6.Text;
                    string Scope = txt_Scope6.Text;
                    string Tcard = txt_Tcard6.Text;
                    int Tcard_Type_ID = Convert.ToInt32(txt_Tcard6.Tag);
                    int Update_YesNo = Convert.ToInt32(radioGroup1.SelectedIndex);
                    string Update_ProblemDescription = txt_Description6.Text;
                    string Update_Responsible = cmb_Responsible6.Text;
                    string Update_ActionPlan = txt_ActionPlan6.Text;
                    int Update_Status;
                    //没有问题时Status等于4(100%)，有问题时根据选择
                    if (radioGroup1.SelectedIndex == 0)
                    {
                        Update_Status = 4;
                    }
                    else
                    {
                        Update_Status = imageComboBoxEdit1.SelectedIndex;
                    }
                    string Update_Comments = txt_Comments6.Text;
                    string Update_Items;
                    string Update_Content;
                    if (dtp62.Text != "")
                    {
                        string CompletionDate = dtp62.DateTime.ToString();
                        Update_Items = "Type,ListID,Date,Auditor,Scope,Tcard,Problem,ProblemDescription,Responsible,ActionPlan,CompletionDate,Status,Comments,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate,AuditorID,ResponsibleID,Tcard_Type_ID";
                        Update_Content = $"'{type}',{ListID},'{Date}','{Auditor}','{Scope}','{Tcard}',{Update_YesNo},'{Update_ProblemDescription}','{Update_Responsible}','{Update_ActionPlan}','{CompletionDate}',{Update_Status},'{Update_Comments}',{SystemInfo.readFactory},{SystemInfo.readDepartment},{SystemInfo.userID},'{DateTime.Now}',{AuditorTag},{ResponsibleTag},{Tcard_Type_ID}";
                    }
                    else
                    {
                        Update_Items = "Type,ListID,Date,Auditor,Scope,Tcard,Problem,ProblemDescription,Responsible,ActionPlan,Status,Comments,AuditFactortyID,AuditDepartmentID,CreatorID,CreationDate,AuditorID,ResponsibleID,Tcard_Type_ID";
                        Update_Content = $"'{type}',{ListID},'{Date}','{Auditor}','{Scope}','{Tcard}',{Update_YesNo},'{Update_ProblemDescription}','{Update_Responsible}','{Update_ActionPlan}',{Update_Status},'{Update_Comments}',{SystemInfo.readFactory},{SystemInfo.readDepartment},{SystemInfo.userID},'{DateTime.Now}',{AuditorTag},{ResponsibleTag},{Tcard_Type_ID}";
                    }
                    SQL.Insert(SystemInfo.SQL, "LPA_ResultList_History", Update_Items, Update_Content);


                    DevExpress.XtraEditors.XtraMessageBox.Show("Update completed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    tab_LPA.SelectedTabPageIndex = 0;
                    LoadResultList();
                    Empty6();
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Unknown error\r\nError code: 102", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Unknown error\r\nError code: 101", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btn_Empty6_Click(object sender, EventArgs e)
        {
            Empty6();
        }
        private void Empty6()
        {
            lb_ListID.Text = "";
            dtp61.Text = "";
            cmb_Auditor6.SelectedIndex = -1;
            radioGroup1.SelectedIndex = -1;
            txt_Description6.Text = "";
            cmb_Responsible6.SelectedIndex = -1;
            txt_ActionPlan6.Text = "";
            dtp62.Text = "";
            imageComboBoxEdit1.SelectedIndex = -1;
            txt_Comments6.Text = "";
        }
        #endregion

        #region Analysis
        private void dtp71_EditValueChanged(object sender, EventArgs e)
        {
            Analysis();
        }
        private void Analysis()
        {
            int month = dtp71.DateTime.Month;
            LoadData_Analysis1();

            //计算YTD
            int a1 = CalculateYTD(gridView_Analysis, 0);// 计算年度计划审核次数
            int a2 = CalculateYTD(gridView_Analysis, 1);// 计算年度实施审核次数
            int a4 = CalculateYTD(gridView_Analysis, 4);// 计算未发现不符合的审核次数
            int a5 = CalculateYTD(gridView_Analysis, 5);// 计算发现不符合的审核 
            gridView_Analysis.SetRowCellValue(0, "TextColumn2", Math.Round(Convert.ToDouble(a1) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis.SetRowCellValue(1, "TextColumn2", Math.Round(Convert.ToDouble(a2) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis.SetRowCellValue(2, "TextColumn2", a1 - a2);
            if (a1 > 0)
            {
                gridView_Analysis.SetRowCellValue(3, "TextColumn2", Math.Round(Convert.ToDouble(a2 * 100) / a1, MidpointRounding.AwayFromZero));
            }
            else
            {
                gridView_Analysis.SetRowCellValue(3, "TextColumn2", 0);
            }
            gridView_Analysis.SetRowCellValue(4, "TextColumn2", Math.Round(Convert.ToDouble(a4) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis.SetRowCellValue(5, "TextColumn2", Math.Round(Convert.ToDouble(a5) / month, MidpointRounding.AwayFromZero));
            if (a2 > 0)
            {
                gridView_Analysis.SetRowCellValue(6, "TextColumn2", Math.Round(Convert.ToDouble(a5 * 100) / a2, MidpointRounding.AwayFromZero));
            }
            else
            {
                gridView_Analysis.SetRowCellValue(6, "TextColumn2", 0);
            }

            gridView_Analysis_Table.SetRowCellValue(0, "TextColumn2", Math.Round(Convert.ToDouble(CalculateYTD(gridView_Analysis_Table, 0)) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis_Table.SetRowCellValue(1, "TextColumn2", Math.Round(Convert.ToDouble(CalculateYTD(gridView_Analysis_Table, 1)) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis_Table.SetRowCellValue(2, "TextColumn2", Math.Round(Convert.ToDouble(CalculateYTD(gridView_Analysis_Table, 2)) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis_Table.SetRowCellValue(3, "TextColumn2", Math.Round(Convert.ToDouble(CalculateYTD(gridView_Analysis_Table, 3)) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis_Table.SetRowCellValue(4, "TextColumn2", Math.Round(Convert.ToDouble(CalculateYTD(gridView_Analysis_Table, 4)) / month, MidpointRounding.AwayFromZero));
            gridView_Analysis_Table.SetRowCellValue(5, "TextColumn2", Math.Round(Convert.ToDouble(CalculateYTD(gridView_Analysis_Table, 5)) / month, MidpointRounding.AwayFromZero));

            LoadData_Chart1();
            LoadData_Chart2();
        }
        private int CalculateYTD(GridView gridView, int RowIndex)
        {
            int Val = 0;
            for (int i = 3; i < 15; i++)
            {
                Val += Convert.ToInt32(gridView.GetRowCellValue(RowIndex, "TextColumn" + i).ToString());
            }
            return Val;
        }
        private void btn_AnalysisBack_Click(object sender, EventArgs e)
        {
            tab_LPA.SelectedTabPageIndex = 0;
        }
        private void LoadGridView_Analysis()
        {
            GridColumn textColumn1 = new GridColumn();
            textColumn1.FieldName = "TextColumn1"; // 设置字段名
            textColumn1.Caption = " "; // 列标题
            textColumn1.Visible = true; // 显示列

            GridColumn textColumn2 = new GridColumn();
            textColumn2.FieldName = "TextColumn2"; // 设置字段名
            textColumn2.Caption = "YTD"; // 列标题
            textColumn2.Visible = true; // 显示列

            GridColumn textColumn3 = new GridColumn();
            textColumn3.FieldName = "TextColumn3"; // 设置字段名
            textColumn3.Caption = "Jan"; // 列标题
            textColumn3.Visible = true; // 显示列        

            GridColumn textColumn4 = new GridColumn();
            textColumn4.FieldName = "TextColumn4"; // 设置字段名
            textColumn4.Caption = "Feb"; // 列标题
            textColumn4.Visible = true; // 显示列

            GridColumn textColumn5 = new GridColumn();
            textColumn5.FieldName = "TextColumn5"; // 设置字段名
            textColumn5.Caption = "Mar"; // 列标题
            textColumn5.Visible = true; // 显示列

            GridColumn textColumn6 = new GridColumn();
            textColumn6.FieldName = "TextColumn6"; // 设置字段名
            textColumn6.Caption = "Apr"; // 列标题
            textColumn6.Visible = true; // 显示列

            GridColumn textColumn7 = new GridColumn();
            textColumn7.FieldName = "TextColumn7"; // 设置字段名
            textColumn7.Caption = "May"; // 列标题
            textColumn7.Visible = true; // 显示列

            GridColumn textColumn8 = new GridColumn();
            textColumn8.FieldName = "TextColumn8"; // 设置字段名
            textColumn8.Caption = "Jun"; // 列标题
            textColumn8.Visible = true; // 显示列

            GridColumn textColumn9 = new GridColumn();
            textColumn9.FieldName = "TextColumn9"; // 设置字段名
            textColumn9.Caption = "Jul"; // 列标题
            textColumn9.Visible = true; // 显示列

            GridColumn textColumn10 = new GridColumn();
            textColumn10.FieldName = "TextColumn10"; // 设置字段名
            textColumn10.Caption = "Aug"; // 列标题
            textColumn10.Visible = true; // 显示列

            GridColumn textColumn11 = new GridColumn();
            textColumn11.FieldName = "TextColumn11"; // 设置字段名
            textColumn11.Caption = "Sep"; // 列标题
            textColumn11.Visible = true; // 显示列

            GridColumn textColumn12 = new GridColumn();
            textColumn12.FieldName = "TextColumn12"; // 设置字段名
            textColumn12.Caption = "Oct"; // 列标题
            textColumn12.Visible = true; // 显示列

            GridColumn textColumn13 = new GridColumn();
            textColumn13.FieldName = "TextColumn13"; // 设置字段名
            textColumn13.Caption = "Nov"; // 列标题
            textColumn13.Visible = true; // 显示列

            GridColumn textColumn14 = new GridColumn();
            textColumn14.FieldName = "TextColumn14"; // 设置字段名
            textColumn14.Caption = "Dec"; // 列标题
            textColumn14.Visible = true; // 显示列

            textColumn1.Width = 100;
            textColumn2.Width = 30;
            textColumn3.Width = 30;
            textColumn4.Width = 30;
            textColumn5.Width = 30;
            textColumn6.Width = 30;
            textColumn7.Width = 30;
            textColumn8.Width = 30;
            textColumn9.Width = 30;
            textColumn10.Width = 30;
            textColumn11.Width = 30;
            textColumn12.Width = 30;
            textColumn13.Width = 30;
            textColumn14.Width = 30;

            gridView_Analysis.Columns.Add(textColumn1);
            gridView_Analysis.Columns.Add(textColumn2);
            gridView_Analysis.Columns.Add(textColumn3);
            gridView_Analysis.Columns.Add(textColumn4);
            gridView_Analysis.Columns.Add(textColumn5);
            gridView_Analysis.Columns.Add(textColumn6);
            gridView_Analysis.Columns.Add(textColumn7);
            gridView_Analysis.Columns.Add(textColumn8);
            gridView_Analysis.Columns.Add(textColumn9);
            gridView_Analysis.Columns.Add(textColumn10);
            gridView_Analysis.Columns.Add(textColumn11);
            gridView_Analysis.Columns.Add(textColumn12);
            gridView_Analysis.Columns.Add(textColumn13);
            gridView_Analysis.Columns.Add(textColumn14);

            // 创建数据源
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("TextColumn1", typeof(string));
            dataTable.Columns.Add("TextColumn2", typeof(string));
            dataTable.Columns.Add("TextColumn3", typeof(string));
            dataTable.Columns.Add("TextColumn4", typeof(string));
            dataTable.Columns.Add("TextColumn5", typeof(string));
            dataTable.Columns.Add("TextColumn6", typeof(string));
            dataTable.Columns.Add("TextColumn7", typeof(string));
            dataTable.Columns.Add("TextColumn8", typeof(string));
            dataTable.Columns.Add("TextColumn9", typeof(string));
            dataTable.Columns.Add("TextColumn10", typeof(string));
            dataTable.Columns.Add("TextColumn11", typeof(string));
            dataTable.Columns.Add("TextColumn12", typeof(string));
            dataTable.Columns.Add("TextColumn13", typeof(string));
            dataTable.Columns.Add("TextColumn14", typeof(string));

            gridControl_Analysis.DataSource = dataTable;

            gridView_Analysis.Appearance.Row.TextOptions.HAlignment = HorzAlignment.Center;
            gridView_Analysis.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;

            for (int i = 0; i < gridView_Analysis.Columns.Count; i++)
            {
                if (i == 0)
                {
                    gridView_Analysis.Columns[i].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
                }
                else if (i == 1)
                {
                    gridView_Analysis.Columns[i].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                    gridView_Analysis.Columns[i].AppearanceHeader.BackColor = Color.FromArgb(237, 140, 0);
                }
                else
                {
                    gridView_Analysis.Columns[i].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                    gridView_Analysis.Columns[i].AppearanceHeader.BackColor = Color.FromArgb(30, 42, 99);
                }
            }
        }
        private void LoadGridView_Analysis_Table()
        {
            GridColumn textColumn1 = new GridColumn();
            textColumn1.FieldName = "TextColumn1"; // 设置字段名
            textColumn1.Caption = "Item"; // 列标题
            textColumn1.Visible = true; // 显示列

            GridColumn textColumn2 = new GridColumn();
            textColumn2.FieldName = "TextColumn2"; // 设置字段名
            textColumn2.Caption = "YTD"; // 列标题
            textColumn2.Visible = true; // 显示列

            GridColumn textColumn3 = new GridColumn();
            textColumn3.FieldName = "TextColumn3"; // 设置字段名
            textColumn3.Caption = "Jan"; // 列标题
            textColumn3.Visible = true; // 显示列        

            GridColumn textColumn4 = new GridColumn();
            textColumn4.FieldName = "TextColumn4"; // 设置字段名
            textColumn4.Caption = "Feb"; // 列标题
            textColumn4.Visible = true; // 显示列

            GridColumn textColumn5 = new GridColumn();
            textColumn5.FieldName = "TextColumn5"; // 设置字段名
            textColumn5.Caption = "Mar"; // 列标题
            textColumn5.Visible = true; // 显示列

            GridColumn textColumn6 = new GridColumn();
            textColumn6.FieldName = "TextColumn6"; // 设置字段名
            textColumn6.Caption = "Apr"; // 列标题
            textColumn6.Visible = true; // 显示列

            GridColumn textColumn7 = new GridColumn();
            textColumn7.FieldName = "TextColumn7"; // 设置字段名
            textColumn7.Caption = "May"; // 列标题
            textColumn7.Visible = true; // 显示列

            GridColumn textColumn8 = new GridColumn();
            textColumn8.FieldName = "TextColumn8"; // 设置字段名
            textColumn8.Caption = "Jun"; // 列标题
            textColumn8.Visible = true; // 显示列

            GridColumn textColumn9 = new GridColumn();
            textColumn9.FieldName = "TextColumn9"; // 设置字段名
            textColumn9.Caption = "Jul"; // 列标题
            textColumn9.Visible = true; // 显示列

            GridColumn textColumn10 = new GridColumn();
            textColumn10.FieldName = "TextColumn10"; // 设置字段名
            textColumn10.Caption = "Aug"; // 列标题
            textColumn10.Visible = true; // 显示列

            GridColumn textColumn11 = new GridColumn();
            textColumn11.FieldName = "TextColumn11"; // 设置字段名
            textColumn11.Caption = "Sep"; // 列标题
            textColumn11.Visible = true; // 显示列

            GridColumn textColumn12 = new GridColumn();
            textColumn12.FieldName = "TextColumn12"; // 设置字段名
            textColumn12.Caption = "Oct"; // 列标题
            textColumn12.Visible = true; // 显示列

            GridColumn textColumn13 = new GridColumn();
            textColumn13.FieldName = "TextColumn13"; // 设置字段名
            textColumn13.Caption = "Nov"; // 列标题
            textColumn13.Visible = true; // 显示列

            GridColumn textColumn14 = new GridColumn();
            textColumn14.FieldName = "TextColumn14"; // 设置字段名
            textColumn14.Caption = "Dec"; // 列标题
            textColumn14.Visible = true; // 显示列


            textColumn1.Width = 100;
            textColumn2.Width = 30;
            textColumn3.Width = 30;
            textColumn4.Width = 30;
            textColumn5.Width = 30;
            textColumn6.Width = 30;
            textColumn7.Width = 30;
            textColumn8.Width = 30;
            textColumn9.Width = 30;
            textColumn10.Width = 30;
            textColumn11.Width = 30;
            textColumn12.Width = 30;
            textColumn13.Width = 30;
            textColumn14.Width = 30;

            gridView_Analysis_Table.Columns.Add(textColumn1);
            gridView_Analysis_Table.Columns.Add(textColumn2);
            gridView_Analysis_Table.Columns.Add(textColumn3);
            gridView_Analysis_Table.Columns.Add(textColumn4);
            gridView_Analysis_Table.Columns.Add(textColumn5);
            gridView_Analysis_Table.Columns.Add(textColumn6);
            gridView_Analysis_Table.Columns.Add(textColumn7);
            gridView_Analysis_Table.Columns.Add(textColumn8);
            gridView_Analysis_Table.Columns.Add(textColumn9);
            gridView_Analysis_Table.Columns.Add(textColumn10);
            gridView_Analysis_Table.Columns.Add(textColumn11);
            gridView_Analysis_Table.Columns.Add(textColumn12);
            gridView_Analysis_Table.Columns.Add(textColumn13);
            gridView_Analysis_Table.Columns.Add(textColumn14);

            // 创建数据源
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("TextColumn1", typeof(string));
            dataTable.Columns.Add("TextColumn2", typeof(string));
            dataTable.Columns.Add("TextColumn3", typeof(string));
            dataTable.Columns.Add("TextColumn4", typeof(string));
            dataTable.Columns.Add("TextColumn5", typeof(string));
            dataTable.Columns.Add("TextColumn6", typeof(string));
            dataTable.Columns.Add("TextColumn7", typeof(string));
            dataTable.Columns.Add("TextColumn8", typeof(string));
            dataTable.Columns.Add("TextColumn9", typeof(string));
            dataTable.Columns.Add("TextColumn10", typeof(string));
            dataTable.Columns.Add("TextColumn11", typeof(string));
            dataTable.Columns.Add("TextColumn12", typeof(string));
            dataTable.Columns.Add("TextColumn13", typeof(string));
            dataTable.Columns.Add("TextColumn14", typeof(string));

            gridControl_Analysis_Table.DataSource = dataTable;
        }
        private void LoadData_Analysis1()
        {
            while (gridView_Analysis.DataRowCount > 0) // 循环删除所有行
            {
                gridView_Analysis.DeleteRow(0);
            }
            while (gridView_Analysis_Table.DataRowCount > 0) // 循环删除所有行
            {
                gridView_Analysis_Table.DeleteRow(0);
            }

            DateTime dateTime1 = new DateTime(dtp71.DateTime.Year, 1, 1);
            DateTime dateTime2 = dateTime1.AddYears(1);
            DataTable dt_YearPlan = SQL.ReturnDataTable3(SystemInfo.SQL, "View_LPA_Plan", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date>='" + dateTime1 + "' and Date<'" + dateTime2 + "' and Del_tag=0");
            DataTable dt_YearList_All = SQL.ReturnDataTable3(SystemInfo.SQL, "View_LPA_Result", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and Date>='" + dateTime1 + "' and Date<'" + dateTime2 + "' and Del_tag=0");
            DataTable dt_YearList_Single = new DataTable();

            // 将年度的审核结果的表进行调整，删除日期和人名列重复的行，避免一个人审核两次的内容出现。
            var uniqueRows = dt_YearList_All.AsEnumerable()
                   .GroupBy(row => new
                   {
                       Column1Value = row.Field<object>("Date"),
                       Column2Value = row.Field<object>("Auditor")
                   })
                   .Select(group => group.First());
            if (uniqueRows.Count() == 0)
            {
                MessageBox.Show("未找到LPA信息");
                return;
            }
            dt_YearList_Single = uniqueRows.CopyToDataTable();


            List<string> list_Plan = new List<string> { "计划审核 [次]" };
            List<string> list_Implementation = new List<string> { "实施审核 [次]" };
            List<string> list_Unimplemented = new List<string> { "未实施审核 [次]" };
            List<string> list_Implementation_Rate = new List<string> { "审核实施率 [%]" };
            List<string> list_NoFind = new List<string> { "未发现不符合的审核 [次]" };
            List<string> list_Find = new List<string> { "发现不符合的审核 [次]" };
            List<string> list_FindRate = new List<string> { "发现不符合的占比 [%]" };

            List<string> list_5S = new List<string> { "5S" };
            List<string> list_HSE = new List<string> { "HSE" };
            List<string> list_可追溯性 = new List<string> { "可追溯性" };
            List<string> list_人员 = new List<string> { "人员" };
            List<string> list_过程一致性 = new List<string> { "过程一致性" };
            List<string> list_产品一致性 = new List<string> { "产品一致性" };


            for (int i = 1; i < 13; i++)
            {
                DateTime dateTime_Month1 = new DateTime(dtp71.DateTime.Year, i, 1);// 每月第一天的日期
                DateTime dateTime_Month2 = dateTime_Month1.AddMonths(1);// 下月第一天的日期
                DataRow[] dr1 = dt_YearPlan.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "'");// 该月计划审核数量
                DataRow[] dr2 = dt_YearList_Single.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "'");// 该月实施审核数量
                DataRow[] dr_5S = dt_YearList_All.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "' and Problem=1 and Item='5S'");// 发现问题中5S类的数量
                DataRow[] dr_HSE = dt_YearList_All.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "' and Problem=1 and Item='HSE'");// 发现问题中5S类的数量
                DataRow[] dr_可追溯性 = dt_YearList_All.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "' and Problem=1 and Item='可追溯性'");// 发现问题中5S类的数量
                DataRow[] dr_人员 = dt_YearList_All.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "' and Problem=1 and Item='人员'");// 发现问题中5S类的数量
                DataRow[] dr_过程一致性 = dt_YearList_All.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "' and Problem=1 and Item='过程一致性'");// 发现问题中5S类的数量
                DataRow[] dr_产品一致性 = dt_YearList_All.Select("Date>='" + dateTime_Month1 + "' and Date<'" + dateTime_Month2 + "' and Problem=1 and Item='产品一致性'");// 发现问题中5S类的数量

                list_Plan.Add(dr1.Length.ToString());
                list_Implementation.Add(dr2.Length.ToString());
                list_Unimplemented.Add((dr1.Length - dr2.Length).ToString());
                if (dr1.Length == 0)
                {
                    list_Implementation_Rate.Add("0");
                }
                else
                {
                    list_Implementation_Rate.Add((dr2.Length * 100 / dr1.Length).ToString());
                }
                int find = dr_5S.Length + dr_HSE.Length + dr_可追溯性.Length + dr_人员.Length + dr_过程一致性.Length + dr_产品一致性.Length;
                list_Find.Add(find.ToString());
                list_NoFind.Add((dr2.Length - find).ToString());
                if (dr2.Length == 0)
                {
                    list_FindRate.Add("0");
                }
                else
                {
                    list_FindRate.Add((find * 100 / dr2.Length).ToString());
                }

                list_5S.Add(dr_5S.Length.ToString());
                list_HSE.Add(dr_HSE.Length.ToString());
                list_可追溯性.Add(dr_可追溯性.Length.ToString());
                list_人员.Add(dr_人员.Length.ToString());
                list_过程一致性.Add(dr_过程一致性.Length.ToString());
                list_产品一致性.Add(dr_产品一致性.Length.ToString());
            }
            LoadData_Analysis2(gridView_Analysis, list_Plan);
            LoadData_Analysis2(gridView_Analysis, list_Implementation);
            LoadData_Analysis2(gridView_Analysis, list_Unimplemented);
            LoadData_Analysis2(gridView_Analysis, list_Implementation_Rate);
            LoadData_Analysis2(gridView_Analysis, list_NoFind);
            LoadData_Analysis2(gridView_Analysis, list_Find);
            LoadData_Analysis2(gridView_Analysis, list_FindRate);

            LoadData_Analysis2(gridView_Analysis_Table, list_5S);
            LoadData_Analysis2(gridView_Analysis_Table, list_HSE);
            LoadData_Analysis2(gridView_Analysis_Table, list_可追溯性);
            LoadData_Analysis2(gridView_Analysis_Table, list_人员);
            LoadData_Analysis2(gridView_Analysis_Table, list_过程一致性);
            LoadData_Analysis2(gridView_Analysis_Table, list_产品一致性);

        }
        private void LoadData_Analysis2(GridView gridView, List<string> list)
        {
            gridView.AddNewRow(); // 添加新行
            int b = gridView.GetRowHandle(gridView.DataRowCount);
            gridView.SetRowCellValue(b, "TextColumn1", list[0].ToString());
            //gridView.SetRowCellValue(b, "TextColumn2", "NA");
            gridView.SetRowCellValue(b, "TextColumn3", list[1].ToString());
            gridView.SetRowCellValue(b, "TextColumn4", list[2].ToString());
            gridView.SetRowCellValue(b, "TextColumn5", list[3].ToString());
            gridView.SetRowCellValue(b, "TextColumn6", list[4].ToString());
            gridView.SetRowCellValue(b, "TextColumn7", list[5].ToString());
            gridView.SetRowCellValue(b, "TextColumn8", list[6].ToString());
            gridView.SetRowCellValue(b, "TextColumn9", list[7].ToString());
            gridView.SetRowCellValue(b, "TextColumn10", list[8].ToString());
            gridView.SetRowCellValue(b, "TextColumn11", list[9].ToString());
            gridView.SetRowCellValue(b, "TextColumn12", list[10].ToString());
            gridView.SetRowCellValue(b, "TextColumn13", list[11].ToString());
            gridView.SetRowCellValue(b, "TextColumn14", list[12].ToString());
            gridView.UpdateCurrentRow(); // 更新当前行
        }
        /// <summary>
        /// 加载柱状图
        /// </summary>
        private void LoadData_Chart1()
        {
            chartControl1.Series[0].Points.Clear();
            chartControl1.Series[1].Points.Clear();
            chartControl1.Series[2].Points.Clear();
            chartControl1.Series[3].Points.Clear();
            chartControl1.Series[4].Points.Clear();
            chartControl1.Series[5].Points.Clear();

            for (int i = 0; i < 6; i++)
            {
                int rowindex = 0;
                if (i == 0)
                {
                    rowindex = 4;
                }
                else if (i == 1)
                {
                    rowindex = 5;
                }
                else if (i == 2)
                {
                    rowindex = 2;
                }
                else if (i == 3)
                {
                    rowindex = 6;
                }
                else if (i == 5)
                {
                    rowindex = 3;
                }
                if (i != 4)
                {
                    chartControl1.Series[i].Points.Add(new SeriesPoint("YTD", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn2").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Jan", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn3").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Feb", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn4").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Mar", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn5").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Apr", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn6").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("May", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn7").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Jun", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn8").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Jul", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn9").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Aug", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn10").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Sep", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn11").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Oct", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn12").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Nov", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn13").ToString()));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Dec", gridView_Analysis.GetRowCellValue(rowindex, "TextColumn14").ToString()));
                }
                else
                {
                    chartControl1.Series[i].Points.Add(new SeriesPoint("YTD", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Jan", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Feb", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Mar", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Apr", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("May", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Jun", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Jul", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Aug", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Sep", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Oct", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Nov", 90));
                    chartControl1.Series[i].Points.Add(new SeriesPoint("Dec", 90));
                }
            }
        }
        /// <summary>
        /// 加载饼图
        /// </summary>
        private void LoadData_Chart2()
        {
            int month = dtp71.DateTime.Month + 2;

            chartControl2.Series[0].Points.Clear();
            chartControl2.Series[1].Points.Clear();
            chartControl2.Series[0].Points.Add(new SeriesPoint("5S", gridView_Analysis_Table.GetRowCellValue(0, "TextColumn" + month)));
            chartControl2.Series[0].Points.Add(new SeriesPoint("HSE", gridView_Analysis_Table.GetRowCellValue(1, "TextColumn" + month)));
            chartControl2.Series[0].Points.Add(new SeriesPoint("可追溯性", gridView_Analysis_Table.GetRowCellValue(2, "TextColumn" + month)));
            chartControl2.Series[0].Points.Add(new SeriesPoint("人员", gridView_Analysis_Table.GetRowCellValue(3, "TextColumn" + month)));
            chartControl2.Series[0].Points.Add(new SeriesPoint("过程一致性", gridView_Analysis_Table.GetRowCellValue(4, "TextColumn" + month)));
            chartControl2.Series[0].Points.Add(new SeriesPoint("产品一致性", gridView_Analysis_Table.GetRowCellValue(5, "TextColumn" + month)));

            chartControl2.Series[1].Points.Add(new SeriesPoint("5S", gridView_Analysis_Table.GetRowCellValue(0, "TextColumn2")));
            chartControl2.Series[1].Points.Add(new SeriesPoint("HSE", gridView_Analysis_Table.GetRowCellValue(1, "TextColumn2")));
            chartControl2.Series[1].Points.Add(new SeriesPoint("可追溯性", gridView_Analysis_Table.GetRowCellValue(2, "TextColumn2")));
            chartControl2.Series[1].Points.Add(new SeriesPoint("人员", gridView_Analysis_Table.GetRowCellValue(3, "TextColumn2")));
            chartControl2.Series[1].Points.Add(new SeriesPoint("过程一致性", gridView_Analysis_Table.GetRowCellValue(4, "TextColumn2")));
            chartControl2.Series[1].Points.Add(new SeriesPoint("产品一致性", gridView_Analysis_Table.GetRowCellValue(5, "TextColumn2")));
        }
        private void btn_Export7_Click(object sender, EventArgs e)
        {
            ExportGridToFixedExcelPosition();
        }
        private void ExportGridToFixedExcelPosition()
        {
            byte[] ResourceFile = Shopfloormanagement.Properties.Resources.分层审核月度总结_V2_0; // 替换为你的资源名称
            using (MemoryStream stream = new MemoryStream(ResourceFile))
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.FileName = "DataAnalysis File";
                saveFileDialog.Title = "导出Excel";
                saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls";
                DialogResult dialogResult = saveFileDialog.ShowDialog();
                if (dialogResult == DialogResult.Cancel)
                    return;
                string FileName = saveFileDialog.FileName;

                SpreadsheetControl sheetControl = new SpreadsheetControl();
                IWorkbook workbook1 = sheetControl.Document;

                // 创建 Workbook 实例并加载现有文件
                workbook1.LoadDocument(stream);

                // 获取目标工作表（例如 "Sheet2"）
                var worksheet = workbook1.Worksheets["Sheet1"];

                // 定义起始位置（例如从 A5 单元格开始）
                int startRow = 0;  // Excel 行号（从1开始）
                int startColumn = 0; // Excel 列号（从0开始，A=0, B=1, ...）

                // 第一个
                var gridView1 = gridView_Analysis;
                if (gridView1 == null) return;

                // 逐行写入数据到指定位置
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    for (int j = 0; j < gridView1.Columns.Count; j++)
                    {
                        // 计算目标单元格位置（例如 A5, A6, A7...）
                        int targetRow = startRow + i;
                        int targetColumn = startColumn + j;

                        // 写入数据
                        if (j == 0)
                        {
                            worksheet.Cells[targetRow, targetColumn].Value = gridView1.GetRowCellValue(i, gridView1.Columns[j]).ToString();
                        }
                        else
                        {
                            worksheet.Cells[targetRow, targetColumn].Value = Convert.ToInt32(gridView1.GetRowCellValue(i, gridView1.Columns[j]).ToString());
                        }
                    }
                }

                // 第二个
                var gridView2 = gridView_Analysis_Table;
                if (gridView2 == null) return;

                // 逐行写入数据到指定位置
                for (int i = 0; i < gridView2.RowCount; i++)
                {
                    for (int j = 0; j < gridView2.Columns.Count; j++)
                    {
                        // 计算目标单元格位置（例如 A5, A6, A7...）
                        int targetRow = 8 + i;
                        int targetColumn = startColumn + j;

                        // 写入数据
                        if (j == 0)
                        {
                            worksheet.Cells[targetRow, targetColumn].Value = gridView2.GetRowCellValue(i, gridView2.Columns[j]).ToString();
                        }
                        else
                        {
                            worksheet.Cells[targetRow, targetColumn].Value = Convert.ToInt32(gridView2.GetRowCellValue(i, gridView2.Columns[j]).ToString());
                        }
                    }
                }
                // 保存文件
                workbook1.SaveDocument(FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("Export successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion
    }
}